UITableView使用技巧总结(删除,多选,滑动等)

一.进行重载数据的API

注意点:在操作这些方法的时候,记得要修改和保存数据来源,根据测试,该方法调用之后,会调用返回组数和行数的数据源方法(不会调用返回cell的数据源方法).倘若不同步刷新和更改数据会造成真实反正行/组合实际添加/删除后的行/组不匹配而奔溃.

插入某行/某组数据:

-(void)insertSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以插入整个章节

-(void)insertRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;

删除某行某族数据:

-(void)deleteSections:(NSIndexSet*)sectionswithRowAnimation:(UITableViewRowAniamtion)animation;//可以删除整个章节

-(void)deleteRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;

刷新(重置)某行数据

-(void)reloadSections:(NSIndexSet*)sections withRowAnimation:(UITableViewRowAniamtion)animation;//在iPhoneos 3.0中重载章节数据(上面两个方法的合并)它们能重载部分的数据,而不会把所有东西都丢掉

-(void)reloadRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;

二.设置和使用系统的cell样式

-(id)initWithStyle:(UITableviewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;

三.系统自带的UITableView的插入和删除

第三方库的代表:https://github.com/MortimerGoro/MGSwipeTableCell

实现UITableView的对应数据源和代理方法.

开启UITableView的编辑模式

使用技巧:[self.tableViewsetEditing:!_tableView.editinganimated:YES];,这样可以设置按钮点击的编辑和关闭.

- (void)setEditing:(BOOL)editing animated:(BOOL)animate;/** TableView 进入或退出编辑状态(TableView 方法). */

确定哪些行的cell可以编辑 (UITableViewDataSource协议中方法),设置为Yes的时候当当前行编辑模式为删除的时候可滑动删除.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

设置cell侧滑的文字

-(NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath

返回侧滑中的项目,这个方法只有返回按钮为删除样式(无返回样式的时候才有用),当这个方法调用了,返回删除按钮文字的方法和,点击原本删除editingStyleForRowAtIndexPath方法不再调用.

- (nullableNSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath

举个栗子:(可以根据indexpath来定制不同的返回样式)

- (nullableNSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath{

UITableViewRowAction*collectRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"收藏么么哒"handler:^(UITableViewRowAction*_Nonnullaction,NSIndexPath*_NonnullindexPath) {

NSLog(@"点击后的回调,不会走commitEditingStyle方法");

}];

collectRowAction.backgroundColor= [UIColororangeColor];

return@[collectRowAction];

}

设置某一行cell的编辑模式 (UITableViewDelegate)协议中方法,不同行可以设置不同的编辑模式,主要为删除和添加(如果不实现,默认为删除)

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

选中删除(或插入)状态之后的操作(数据源进行更新, cell删除或插入)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;

举个栗子:

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

/**点击删除按钮的操作*/

if(editingStyle ==UITableViewCellEditingStyleDelete){

/**<判断编辑状态是删除时. */

/** 1.更新数据源(数组):根据indexPaht.row作为数组下标,从数组中删除数据. */

[self.arr removeObjectAtIndex:indexPath.row];

/** 2. TableView中删除一个cell. */

[tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationRight]; }

/**点击+号图标的操作. */

if(editingStyle ==UITableViewCellEditingStyleInsert){

/**<判断编辑状态是插入时. *//** 1.更新数据源:向数组中添加数据. */

[self.arr insertObject:@"abcd"atIndex:indexPath.row];

/** 2. TableView中插入一个cell. */

[tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

  }

}

UITableView cell的多选和全选功能:

真正项目中做删除

1.将表中的cell删除

2.将本地的数组中数据删除

3.最后将服务器端的数据删除

多选的步骤

1.允许多行选中self.tableView.allowsMultipleSelectionDuringEditing = YES;开启编辑模式(同上第一点).

2.选择编辑处理方式,默认为删除,这样选择,会返回空心的小圆.

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath

{

             returnUITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;

}

更新数据来源默认为删除,这样选择,会返回空心的小圆.

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

         //self.deleteArr用来定义存放多选选中的cell,indexpath,这里无需手动调用取消选中cell的方法

          [self.deleteArraddObject:indexPath];

}

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

//将数据移除          

           [self.deleteArrremoveObject:indexPath];

}

然后在点击确认删除后使用删除cell的方法(-(void)deleteRowsAtIndexPaths:(NSArray*)indexPahts withRowAnimation:(UITableViewRowAniamtion)animation;)进行删除self.deleteArr中选中的数据.

(注意记得先删除数据来源中的数据,这里可以可以使用直接删除数据来源数组中对应的数据,然后调用reloadData方法,但是这里没有动画),这样的话,self.deleteArr中应该保存的是数据来源数组中的数据[self.deleteArraddObject:[self.dataArrayobjectAtIndex:indexPath.row]];,然后调用([self.deleteArr addObjectsFromArray:self.dataArray];)这个方法

全选功能:

在全选按钮点击后(打开编辑状态),遍历所有数据,调用selectRowAtIndexPath方法,选中多有的cell,同时将全部数据添加到要删除的数组中

for(inti =0; i<self.dataArray.count;i++){

         NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

         [self.tableView selectRowAtIndexPath:indexPath animated:YESscrollPosition:UITableViewScrollPositionTop];

         [self.deleteArr addObjectsFromArray:self.dataArray];

}

参考:http://www.jianshu.com/p/ec6a037e4c6b

四.UITableView cell 的移动

开启UITableView的编辑模式

使用技巧:[self.tableViewsetEditing:!_tableView.editinganimated:YES];,这样可以设置按钮点击的编辑和关闭.

- (void)setEditing:(BOOL)editing animated:(BOOL)animate;/** TableView 进入或退出编辑状态(TableView 方法). */

指定UITableView哪些行可以移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

移动cell

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

举个栗子:

- (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath

{

/**  1.从原位置移除,在从原位置移除之前,需要保存一下原位置的数据,同时持有一次. */

NSString*str = [self.dataArrobjectAtIndex:sourceIndexPath.row];

[self.dataArrremoveObjectAtIndex:sourceIndexPath.row];

/** 2.添加到目的位置,同时释放一次*/

[self.dataArrinsertObject:stratIndex:destinationIndexPath.row];

}

防止不同分区间的移动

- (NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath

- (NSIndexPath*)tableView:(UITableView*)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath*)sourceIndexPath toProposedIndexPath:(NSIndexPath*)proposedDestinationIndexPath{if(sourceIndexPath.section == proposedDestinationIndexPath.section) {returnproposedDestinationIndexPath;    }else{returnsourceIndexPath;    }}

五.UITableView cell 的将要绘制和消失时调用的两个方法

将要出现的时候调用,他的调用顺序在cellforRow方法之后,所以,这个方法,倘若你没有来得及在cellForRow中赋值,还可以在这个方法中救场.这个方法可以用来优化性能,在这里可以监听cell是不是最后一组的最后一行,来判断是否滑动到了tableView的底部(这个在cell的自动计算中拿不到真实contentSize的时候作用巨大)

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath;

cell消失的时候,调用

- (void)tableView:(UITableView*)tableView didEndDisplayingCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath;

六.UITableView cell 传按钮事件给UITableViewController的方法

1.一般来讲,我们可以使用代理或者是Block,但是在有些时候,我们的cell特殊的在其他地方很少用到,甚至不会用到,我们可以直接在

控制器中使用addTarget的方法来简化代码.(在这里需要拿到按钮所在的cell的indexpath)

先展示两个重要的方法

根据indexpath返回tableView对应的cell

- (nullable__kindofUITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath;

根据cell返回其对应的indexPath

- (nullableNSIndexPath*)indexPathForCell:(UICollectionViewCell*)cell;

这里提供两种解决思路:

1.绑定按钮的Tag

在cellforRow方法中对cell中按钮绑定tag,当然这里的用的,使用于cell中只有一个按钮的时候.

可以对btn如此设置:

NSString* cellStr = [NSString stringWithFormat:@"%d", indexPath.row];

[imgBtn setTitle:cellStr forState:UIControlEventTouchCancel];

然后在btn的action函数里边这样调用:

NSString* cellIndex = [imgBtn titleForState:UIControlEventTouchCancel];

取[cellIndex intValue]即可

剥离cell的结构,这个一定要,对自己的cell结构了解,才能取到其cell

NSIntegerremoveRowN = [self.tableViewindexPathForCell:((YGMemberCell*)[[sendersuperview]superview])].row;//这个方便一点点,


待续~~~~~~~~~~~~~~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,670评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,928评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,926评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,238评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,112评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,138评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,545评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,232评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,496评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,596评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,369评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,226评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,600评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,906评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,185评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,516评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,721评论 2 335

推荐阅读更多精彩内容