iOS UITableViewCell取消與全選狀態(tài)

UITableViewCell取消與全選狀態(tài)#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property? (nonatomic, assign)BOOL isAllSelected;

@property (nonatomic,? strong)NSArray? *dataArray;

@property (nonatomic,? strong)UITableView? *tableView;

@property (nonatomic, strong)NSMutableArray *selectedArrM;

@property (strong, nonatomic) NSMutableArray? *selectIndexs; //多選選中的行

//@property (nonatomic, assign)BOOL isSelected;

@end

@implementation ViewController

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? self.title = @"多選cellDemo";

? ? self.selectedArrM = [NSMutableArray array];


? ? self.dataArray = @[@"2",@"3",@"4",@"44565",@"6",@"7",@"8",@"9",@"0",@"13",@"12",@"14",@"15",@"17",@"19",@"20"];

? ? //為左邊的item設(shè)置標(biāo)題和動作

? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"全選" style:UIBarButtonItemStylePlain target:self action:@selector(selecteAllCells:)];

? ? [self customTableView];

}

- (void)customTableView{

? ? self.tableView=? [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

? ? self.tableView.delegate=self;

? ? self.tableView.dataSource=? self;

//? ? self.tableView.editing = YES;

//? ? self.tableView.allowsMultipleSelectionDuringEditing = YES;

? ? //rself.tableView = tableV;


? ? [self.view addSubview:self.tableView ];

}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

? ? return self.dataArray.count;

}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

? ? UITableViewCell? *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableViewCell"];

? ? if(cell ==nil) {

? ? ? ? cell =? [[UITableViewCell? alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];

? ? }

? ? cell.accessoryType = UITableViewCellAccessoryNone;

? ? for(NSIndexPath*indexinself.selectIndexs) {

? ? ? ? if(index ==? indexPath) {

? ? ? ? ? ? cell.accessoryType? =UITableViewCellAccessoryCheckmark;

? ? ? ? ? ? break;

? ? ? ? }

? ? }

? ? cell.textLabel.text=self.dataArray[indexPath.row];

? ? returncell;

}

//獲取tableView上面所有的cell

- (NSArray*)cellsForTableView:(UITableView*)tableView{

? ? NSIntegersections = tableView.numberOfSections;

? ? NSMutableArray *cells = [[NSMutableArray alloc]init];

?? ? for(intsection =0; section < sections; section++) {

?? ? NSIntegerrows = [tableView? numberOfRowsInSection:section];

?? ? for(introw =0; row < rows; row++) {

? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];

?? ? ? ? [cellsaddObject:[tableViewcellForRowAtIndexPath:indexPath]];

? ? ? ? ? }

?? ? }

? ? returncells;

}

#pragma mark Actions

- (void)selecteAllCells:(UIBarButtonItem*)sender {

? ? NSArray *arr = [self.tableView indexPathsForVisibleRows];

? ? if (_isAllSelected == YES) {

? ? ? ? _isAllSelected = NO;

? ? ? ? [sendersetTitle:@"取消"];

? ? ? ? for(NSIndexPath*indexPathinarr) {

? ? ? ? ? ? //根據(jù)索引观游,獲取cell 然后就可以做你想做的事情啦

? ? ? ? ? ? UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

? ? ? ? ? ? cell.accessoryType = UITableViewCellAccessoryNone; //切換為選中


? ? ? ? }

? ? ? ? for(inti =0; i

? ? ? ? ? ? [self.selectedArrM? removeObject:self.dataArray[i]];

? ? ? ? ? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

? ? ? ? ? ? [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

? ? ? ? }

? ? }else{

? ? ? ? _isAllSelected = YES;

? ? ? ? [sendersetTitle:@"全選"];

? ? ? ? for(NSIndexPath*indexPathinarr) {

? ? ? ? ? ? //根據(jù)索引胯盯,獲取cell 然后就可以做你想做的事情啦

? ? ? ? ? ? UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

? ? ? ? ? ? cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中


? ? ? ? }

? ? ? ? for(inti =0; i

? ? ? ? ? ? [self.selectedArrMaddObject:self.dataArray[i]];

? ? ? ? ? ? NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];

? ? ? ? ? ? [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

? ? ? ? }

? ? }

? ? NSLog(@"=====%@", self.selectedArrM);

}

#pragma mark <UITableViewDelegate>

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

?? ? NSArray*subviews = [[tableViewcellForRowAtIndexPath:indexPath]subviews];

? ? ? ? NSString* name =self.dataArray[indexPath.row];


? ? UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

? ? if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果為選中狀態(tài)

? ? ? ? cell.accessoryType = UITableViewCellAccessoryNone; //切換為未選中

? ? ? ? [self.selectedArrMremoveObject:name];

? ? ? ? [_selectIndexsremoveObject:indexPath];//數(shù)據(jù)移除

? ? }else{//未選中

? ? ? ? cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中

? ? ? ? [self.selectedArrMaddObject:name];

? ? ? ? [_selectIndexsaddObject:indexPath];//添加索引數(shù)據(jù)到數(shù)組

? ? }

? ? NSLog(@"0000====%@",self.selectedArrM);

}

- (NSMutableArray*)selectIndexs{

? ? if (_selectIndexs == nil) {

? ? ? ? _selectIndexs = [NSMutableArray array];

? ? }

? ? return _selectIndexs;

}

@end

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蹦掐,隨后出現(xiàn)的幾起案子嫉柴,更是在濱河造成了極大的恐慌硅卢,老刑警劉巖低飒,帶你破解...
    沈念sama閱讀 223,126評論 6 520
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異搬味,居然都是意外死亡境氢,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,421評論 3 400
  • 文/潘曉璐 我一進(jìn)店門碰纬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來萍聊,“玉大人,你說我怎么就攤上這事悦析∈俳埃” “怎么了?”我有些...
    開封第一講書人閱讀 169,941評論 0 366
  • 文/不壞的土叔 我叫張陵强戴,是天一觀的道長亭螟。 經(jīng)常有香客問我,道長骑歹,這世上最難降的妖魔是什么预烙? 我笑而不...
    開封第一講書人閱讀 60,294評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮道媚,結(jié)果婚禮上扁掸,老公的妹妹穿的比我還像新娘。我一直安慰自己最域,他們只是感情好谴分,可當(dāng)我...
    茶點故事閱讀 69,295評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著镀脂,像睡著了一般牺蹄。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上薄翅,一...
    開封第一講書人閱讀 52,874評論 1 314
  • 那天沙兰,我揣著相機與錄音,去河邊找鬼翘魄。 笑死鼎天,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的熟丸。 我是一名探鬼主播训措,決...
    沈念sama閱讀 41,285評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼伪节,長吁一口氣:“原來是場噩夢啊……” “哼光羞!你這毒婦竟也來了绩鸣?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,249評論 0 277
  • 序言:老撾萬榮一對情侶失蹤纱兑,失蹤者是張志新(化名)和其女友劉穎呀闻,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體潜慎,經(jīng)...
    沈念sama閱讀 46,760評論 1 321
  • 正文 獨居荒郊野嶺守林人離奇死亡捡多,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,840評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了铐炫。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片垒手。...
    茶點故事閱讀 40,973評論 1 354
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖倒信,靈堂內(nèi)的尸體忽然破棺而出科贬,到底是詐尸還是另有隱情,我是刑警寧澤鳖悠,帶...
    沈念sama閱讀 36,631評論 5 351
  • 正文 年R本政府宣布榜掌,位于F島的核電站,受9級特大地震影響乘综,放射性物質(zhì)發(fā)生泄漏憎账。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,315評論 3 336
  • 文/蒙蒙 一卡辰、第九天 我趴在偏房一處隱蔽的房頂上張望胞皱。 院中可真熱鬧,春花似錦看政、人聲如沸朴恳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,797評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽于颖。三九已至,卻和暖如春嚷兔,著一層夾襖步出監(jiān)牢的瞬間森渐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,926評論 1 275
  • 我被黑心中介騙來泰國打工冒晰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留同衣,地道東北人。 一個月前我還...
    沈念sama閱讀 49,431評論 3 379
  • 正文 我出身青樓壶运,卻偏偏與公主長得像耐齐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,982評論 2 361

推薦閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件埠况,我們平時使用的軟件中到處都可以看到它的影子耸携,類似...
    liudhkk閱讀 9,067評論 3 38
  • 一、簡介 <<UITableView(或簡單地說辕翰,表視圖)的一個實例是用于顯示和編輯分層列出的信息的一種手段 <<...
    無邪8閱讀 10,614評論 3 3
  • 前言 最近忙完項目比較閑夺衍,想寫一篇博客來分享一些自學(xué)iOS的心得體會,希望對迷茫的你有所幫助喜命。博主非科班出身沟沙,一些...
    GitHubPorter閱讀 1,438評論 9 5
  • 人是個思想復(fù)雜的生物,有太多自相矛盾的觀念和想法壁榕。 比如我們總是在羨慕別人如何如何矛紫,羨慕別人比自己優(yōu)秀美貌聰明多金...
    書癡的作家夢閱讀 970評論 0 1
  • 呵呵
    這怎么辦閱讀 176評論 0 1