tableView 的cell功能操作方法

批量操作方法

// 允许在编辑模式进行多选操作

    self.tableView.allowsMultipleSelectionDuringEditing = YES;

图1
- (IBAction)remove {

    // 获得所有被选中的行

    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];

    // 便利所有的行号

    NSMutableArray *deletedDeals = [NSMutableArray array];

    for (NSIndexPath *path in indexPaths) {

        [deletedDeals addObject:self.deals[path.row]];

    }


    // 删除模型数据

    [self.deals removeObjectsInArray:deletedDeals];


    // 刷新表格  一定要刷新数据

    [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];

}
---------------------------华丽的分割线------------------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // 取消选中

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
开启编辑
[self.tableView setEditing:!self.tableView.editing animated:YES]; // 有动画

/**

 *  返回indexPath对应的编辑样式

 */

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

    if (tableView.isEditing) { // 如果是编辑模式,则返回插入操作

        return UITableViewCellEditingStyleInsert;

    }

    return UITableViewCellEditingStyleDelete;

}

//哪几行可以编辑

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

    if (indexPath.row > 4) {

        return NO;

    }

    return YES;

}
---------------------------华丽的分割线------------------------

移动cell的方法


图2
//移动cell方法

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

    //   如果原始位置与目标位置不相等   移动模型数组里的数据

    if (sourceIndexPath != destinationIndexPath) {

        //取出原始位置移动的那一行的模型

        id object = [self.contacts objectAtIndex:sourceIndexPath.row];

        //删除  数组中原始索引对应的模型    object

        [self.contacts removeObjectAtIndex:sourceIndexPath.row];

        if(destinationIndexPath.row > [self.contacts count]) { //如果目标位置大于数组的数量由于之前移除了一个模型  所以这里刚好模型数组的数量也减了1

            //如果目标位置大于数组数  就把模型直接添加到最后一个位置

            [self.contacts addObject:object];

        }

        else {

            //如果目标位置小于数组数  就把模型数据插入到数组对应目标位置的地方

            [self.contacts insertObject:object atIndex:destinationIndexPath.row];

        }

    }


    //更新数据

    [NSKeyedArchiver archiveRootObject:self.contacts toFile:FilePath];


}
//哪几行可以移动(可移动的行数小于等于可编辑的行数)

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

    if (indexPath.row > 5) {

        return NO;

    }

    return YES;

}
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {


        UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnullaction, NSIndexPath * _NonnullindexPath) {


                NSLog(@"已删除");

            }];w


        UITableViewRowAction *actionSign = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注微信" handler:^(UITableViewRowAction * _Nonnullaction, NSIndexPath * _NonnullindexPath) {


                NSLog(@"标记关注");

            }];

        actionSign.backgroundColor = [UIColor blueColor];


        return @[action,actionSign];

}

全选按钮操作

- (void)allSelect:(UIButton*)sender{
    NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[table indexPathsForVisibleRows]];
    for (int i = 0; i < [anArrayOfIndexPath count]; i++) {
        NSIndexPath *indexPath= [anArrayOfIndexPath objectAtIndex:i];
        LTableViewCell *cell = (LTableViewCell*)[table cellForRowAtIndexPath:indexPath];
        NSUInteger row = [indexPath row];
        NSLog(@"%lu",(unsigned long)row);
        NSMutableDictionary *dic = [contacts objectAtIndex:row];
        if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]) {
            [dic setObject:@"YES" forKey:@"checked"];
            [cell setChecked:YES];
        }else {
            [dic setObject:@"NO" forKey:@"checked"];
            [cell setChecked:NO];
        }
    }
    if ([[[(UIButton*)sender titleLabel] text] isEqualToString:@"全选"]){
        for (NSDictionary *dic in contacts) {
            [dic setValue:@"YES" forKey:@"checked"];
        }
        [(UIButton*)sender setTitle:@"取消" forState:UIControlStateNormal];
    }else{
        for (NSDictionary *dic in contacts) {
            [dic setValue:@"NO" forKey:@"checked"];
        }
        [(UIButton*)sender setTitle:@"全选" forState:UIControlStateNormal];
    }
}

一旦当tabelView的cell被选中的时候 cell的子控件都会进入高亮状态,通过高亮状态可以设置cell子控件的图片和文字

 // 设置高亮图片(cell选中 -> cell.imageView.highlighted = YES -> cell.imageView显示highlightedImage这个图片)   
cell.imageView.highlightedImage = [UIImage imageNamed:c.highlighted_icon];      
 // 设置label高亮时的文字颜色
cell.textLabel.highlightedTextColor = [UIColor redColor];


文/枫林晚_VG(简书作者)
原文链接:http://www.jianshu.com/p/54b9123ae42f
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值