(四十二)tableView的滑动编辑和刷新 -局部刷新和删除刷新 -待解决问题

tableView的局部刷新有两个方法:

注意这个方法只能用于模型数据的行数不变,否则会出错。

[self.tableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]

对于删除数据的刷新(行数改变),应该调用下面的方法:注意传入的是indexPath数组。

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
Tip:模型数据删掉的个数要和indexPath数组的长度相同。


删除数据的代理方法:只要实现了这个方法就可以进行局部刷新。

/**
 *  滑动删除和编辑功能
 *
 *  @param tableView    要操作的tableView
 *  @param editingStyle 编辑还是删除
 *  @param indexPath    编辑和删除的位置
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.RecentlyDB.RecentlyArray removeObjectAtIndex:indexPath.row];
    }
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

Tip:对于UIBarButtonItem,没有addTarget方法,需要分别设定target(面向的控制器)和action(selector包装的方法)。

tableView进入编辑状态:

[self.tableView setEditing:YES animated:YES];

编辑状态的样式:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleInsert;
}

编辑状态的实质是移动contentView。

有时候编辑状态进入后不出现减号

原文地址:https://www.cnblogs.com/aiwz/p/6154209.html