美文网首页ios 开发收藏
UITableView使用技巧总结(删除,多选,滑动等)

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

作者: 讨厌下雨的鱼 | 来源:发表于2017-04-20 09:27 被阅读276次

    一.进行重载数据的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;//这个方便一点点,

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

    相关文章

      网友评论

        本文标题:UITableView使用技巧总结(删除,多选,滑动等)

        本文链接:https://www.haomeiwen.com/subject/lwdwattx.html