ios开发:tableView的编辑删除cell的方法

#pragma mark - 编辑按钮的方法
    //如何更改UITableView的编辑状态
//    _tableView.editing  默认NO
    //设置UITableView的编辑状态
    [_tableView setEditing:!_tableView.editing animated:YES]; 
    //只有UITableView处于可编辑状态时,才能进行删除、移动等操作
    //NO->YES->NO
#pragma mark - 刷新按钮的方法
- (void)refreshButtonClick:(UIBarButtonItem *)button
    //刷新数据
    //reloadData  会把UITableView所有的代理方法都会重新执行一次
    [_tableView reloadData]; 
//更改删除文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}
//设置UITableView的编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //返回删除类型
        return UITableViewCellEditingStyleDelete;
    }
    //返回新增元素类型
    return UITableViewCellEditingStyleInsert;
}
//单选删除及插入元素对应的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //有关删除元素的方法实现
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //<1>更改数据源
        [[_dataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
        //<2>删除对应多余的cell
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    //有关新增元素的方法实现
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        //<1>更改数据源
        PersonModel *model = [[PersonModel alloc] init];
        model.name = @"我是新来的";
        [[_dataArray objectAtIndex:indexPath.section] insertObject:model atIndex:indexPath.row];
        //<2>新增一行的cell
        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
#pragma mark - 有关移动元素的方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    //sourceIndexPath        原始数据
    //destinationIndexPath   结果的数据 
    //<1>保存将要移动的那行cell上面的数据
    PersonModel *model = [[_dataArray objectAtIndex:sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
    //<2>从原始位置上删除
    [[_dataArray objectAtIndex:sourceIndexPath.section] removeObject:model];
    //<3>放到新的位置上
    [[_dataArray objectAtIndex:destinationIndexPath.section] insertObject:model atIndex:destinationIndexPath.row];
    [_tableView reloadData];
}

#pragma mark - 系统的编辑按钮的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
//    editing  NO->YES->NO
    //设置UITableView的编辑状态
   [_tableView setEditing:editing animated:YES];//==[_tableView setEditing:!_tableView.editing animated:YES]
    [_tableView setEditing:editing animated:YES];
    //清空之前选择的元素
    [_rubbishArray removeAllObjects];************
    if (editing) {
        [self.navigationItem setRightBarButtonItem:_rubbishButton animated:YES];
    }else{
        [self.navigationItem setRightBarButtonItem:nil animated:YES];
    }
}
#pragma mark - 一键删除的按钮的方法
- (void)rubbishButtonClick:(UIBarButtonItem *)button{
    //清除选中的待删除的数据
    [_dataArray removeObjectsInArray:_rubbishArray];
    //tableView刷新数据
    [_tableView reloadData];
#pragma mark - 多选删除的相关方法
//设置编辑类型
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果删除和插入同时返回,就会出现多选删除
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//多选删除对应的方法实现
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //当tableView处于编辑状态时
    if (tableView.editing) {
        //将选中的那行cell上面的数据放到将要删除的数组里面
        [_rubbishArray addObject:[_dataArray objectAtIndex:indexPath.row]];
    }else{
        //当tableView处于不可编辑状态时,爱干嘛干嘛
        NSLog(@"hhhh");
    }
}
//cell的反选
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.editing) {
        [_rubbishArray removeObject:[_dataArray objectAtIndex:indexPath.row]];
    }
}

系统Cell上直接叠加控件

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
    //需要添加else判断,把cell上面的控件清除否则会出现覆盖的情况
    else{
        //cell.contentView.subviews  将cell上面所有的控件放到数组里面
        while ([cell.contentView.subviews lastObject] != nil) {
            //将最后一个元素移除除去;删除Cell上的控件而不是cell清空
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    //将控件添加到cell上面
    [cell.contentView addSubview:titleLable];

注册cell不能接收代理传过来的值

    //注册cell类:xib类型
    [_tableView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"iden"];
    //UITableViewCell的写法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"iden" forIndexPath:indexPath];
    BookModel *model = _dataArray[indexPath.row];
    cell.titleLabel.text = model.title;
    return cell;
}
tableView的cell滑动,显示置顶,删除,或者收藏
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *toTop = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"置顶");
        [tableView setEditing:NO animated:YES];
    }];
    toTop.backgroundColor =[UIColor redColor];
    
    UITableViewRowAction *delege = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        NSLog(@"删除");
        [tableView setEditing:NO animated:YES];
    }];
    delege.backgroundColor =[UIColor greenColor];
    NSArray *arrar = [NSArray arrayWithObjects:toTop,delege, nil];
    return arrar;
}
tableView的编辑状态下全选,修改每个cell左侧的选择按钮的背景图片 http://www.cocoachina.com/bbs/read.php?tid=248799
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    NSLog(@"%@",cell.subviews); //先看看它的子视图,发现只有一个子视图,叫"<uitableviewcellscrollview: 0x8c8e830;="""" frame=""(0"" 0;="""" 320="""" 100);然后你就取到这个uiview,再看它的子视图。="" <br="">
    UIView * view1 = cell.subviews[0];
    NSLog(@"%@",view1.subviews);/*然后这里你看到它有3个子视图,分别是 
   "<uitableviewcellselectedbackground: 0x89aca50;="""" frame=""(0"" -0.5;="""" 320="""" 101);="""" layer=""<CALayer:"" 0x89acb20="""">>",
   "<uitableviewcellcontentview: 0x899c350;="""" frame=""(38"" 0;="""" 282="""" 100);="""" opaque=""NO;"" gesturerecognizers=""<NSArray:"" 0x899c660="""">; layer = <calayer: 0x899c3c0="""">>",
   "<uitableviewcelleditcontrol: 0x8979da0;="""" frame=""(0"" 0;="""" 47="""" 100);="""" opaque=""NO;"" userinteractionenabled=""NO;"" layer=""<CALayer:"" 0x8993960="""">>"
   你一个是你点击了cell之后cell背景变成淡蓝色的背景view,第二个是cell里面常用的contentView,cell的textLabel就是放在这里面的,第三个就是我们要找的,其实这些都是摸索的,我是把第三个view取到,然后把它颜色设成红色,你就会看到那个蓝色的图标就在这个红色区域里,所以可以确定蓝色图标是这个view的子视图,所以从这里继续想下取*/
    UIView * CellEditControl = view1.subviews[2];//按输出顺序来,就是这样向下查找,知道找到你想要的那个view
    UIImageView * imgView = [[[[cell.subviews[0] subviews] objectAtIndex:2] subviews] objectAtIndex:0];
    UIImageView * testImgView = [[UIImageView alloc]initWithImage:imgView.image];
    testImgView.frame = CGRectMake(60, 0, 30, 25);
    [cell.contentView addSubview:testImgView];

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

推荐阅读更多精彩内容