iOS - UITableView 新建、删除

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.delegate && [self.delegate respondsToSelector:@selector(editWords:dic:)]) {
        [self.delegate  editWords:self.canEditing dic:self.dataArr[indexPath.row]];
    }

}

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

    return   UITableViewCellEditingStyleDelete;
}

// 改变删除按钮的title
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return @"删除";
}


// 是否可以左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.canEditing == can) {
        return YES;
    }
    return NO;
}


// 删除用到的函数
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
 if (editingStyle == UITableViewCellEditingStyleDelete)
    {

    [TeacherRequestM paperDeleteWorkworkId:[self.dataArr[indexPath.section] getStringValueForKey:@"id" defaultValue:@" "] SuccessBlock:^{
            NSMutableArray *copy=[self.dataArr mutableCopy];
            [copy  removeObjectAtIndex:indexPath.row];  //删除数组里的数据
            self.dataArr = copy;
            [self.tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//删除对应数据的cell
            [self.tableV reloadData];
        } failBlock:^(NSError *error) {
        }];

    }

}

#pragma mark -- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArr[indexPath.row];
    NSString *CellIdentifier = [NSString stringWithFormat:@"%@%@",StudentDissertationInfoCellIdentifer,[dic getStringValueForKey:@"id" defaultValue:nil]];
    StudentDissertationInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[StudentDissertationInfoCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dic = dic;
    return cell;
}
  

  // 添加整个tableV
    [self addSubview:self.tableV];
    self.tableV.tableHeaderView = self.teacherTableHeaderV;
    self.tableV.tableFooterView = footerV;
    __weak typeof(self) weakSelf = self;
    self.tableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf initData];
    }];
    self.tableV.mj_header.lastUpdatedTimeKey = @"StudentDissertationV";
    [self.tableV.mj_header beginRefreshing];


// 整个tableV
-(UITableView *)tableV{
    if (!_tableV) {
        _tableV = [[UITableView alloc]initWithFrame:CGRM(0,0, S_W,self.height)];
        _tableV.backgroundColor = DefaultBackGroundColor;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        _tableV.showsHorizontalScrollIndicator = NO;
        _tableV.contentInset = UIEdgeInsetsMake(0, 0,10, 0);
        _tableV.separatorColor = [UIColor clearColor];
        _tableV.allowsSelectionDuringEditing = YES;
    }
    return _tableV;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容