ios UITableViewcell上的按钮插入cell

来源:互联网 发布:云技术与大数据 编辑:程序博客网 时间:2024/05/20 05:09

1.我的按钮是在tabView的section上的,点击一下插入俩个cell,二次点击删除这俩个cell

isInsert是按钮的点击状态
countArr是存放我本地图片’名称’的数组,便于刷新的时候从数组中取出名称给本地图片命名
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    if (section ==0) {        return 4;    }else if (section == 1){        return countArr.count;    }else if(section == 2){        return 1;    }    return 0;}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *mycell = @"cell";    //cellForRowAtIndexPath 这个方法不是重用,不要随便用,我这界面内容少,而且大部分是死的,所以我采用的    HTApplyCoinsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];    if (cell == nil) {        cell = [[HTApplyCoinsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mycell];    }     if (indexPath.section == 1){        cell.labTitle.hidden = YES;        cell.textfield.hidden = YES;        cell.imgdisplay.hidden = NO;        cell.imgdisplay.image = [UIImage imageNamed:countArr[indexPath.row]];    }    //cell.selectionStyle = UITableViewCellSelectionStyleNone;    return cell;}
#pragma mark 按钮的点击  重点就在这里面哈-(void)btnDisplayCertificate{    if (!isInsert) {        isInsert = YES;        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];        for (int i=0; i<2; i++) {            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:1];            NSString *picStr = [NSString stringWithFormat:@"图%d",i+1];            NSLog(@"img == %@",picStr);            [countArr addObject:picStr];            [indexPaths addObject: indexPath];        }        //此处是插入 俩个cell        [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];    }else{        isInsert = NO;        NSMutableArray *indexPaths = [[NSMutableArray alloc] init];        [countArr removeAllObjects];        for (int i=0; i<2; i++) {            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:1];            [indexPaths addObject: indexPath];        }        //此处是删除 俩个cell        [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];        [self.tableView beginUpdates];        [self.tableView endUpdates];    }}

希望能对你们有帮助

阅读全文
0 0
原创粉丝点击