iOS-TableView编辑(多行编辑)

来源:互联网 发布:淘宝店铺客服几点上班 编辑:程序博客网 时间:2024/05/16 17:34

TableView编辑(多行编辑)

Demo下载地址- 插入链接
[ 下载地址 ]

github下载地址:https://github.com/7General/HZTableView_selectAll_single


OC的tableview的选中、全选、取消全选demo,(纯代码手动打造)

洲洲哥的github主页。 —— [ 洲洲哥 ]

项目结构

系统界面

编辑界面

全选功能和取消全选功能可以看一下demo

单选功能实现(Block)

/** *  cell内容 */-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    SelectCell * cell = [SelectCell cellWithTableView:tableView];    NSString * keyStr = self.editData[indexPath.section];    NSString * titleLabel = ((NSArray *)self.sectionData[keyStr])[indexPath.row];    cell.titleLabel.text  = titleLabel;    cell.selectButton.selected = ([self.selectArry indexOfObject:indexPath] != NSNotFound);    // 选中按钮    cell.selectButtonClick = ^(UIButton * btn) {        [btn setSelected:!btn.selected];        id view = [btn superview];        NSIndexPath * indexPath = [self.editTableView indexPathForCell:view];        if (btn.selected) {            if ([self.selectArry indexOfObject:indexPath] == NSNotFound) {                [self.selectArry addObject:indexPath];            }        }else {            if ([self.selectArry indexOfObject:indexPath] != NSNotFound) {                [self.selectArry removeObject:indexPath];            }        }    };    return cell;}

全选按钮

// 遍历所有的section下面的cell获取未显示的cell置为选中状态- (void)allSelect{    // 获取所有的section    NSInteger sectionCount = self.editData.count;    // 获取所有cell的indexpath    for (NSInteger Sectionindex = 0; Sectionindex < sectionCount; Sectionindex++) {        NSString * keyStr = self.editData[Sectionindex];        NSInteger cellCount = [self.sectionData[keyStr] count];        for (NSInteger indexCell = 0; indexCell < cellCount; indexCell++) {            NSIndexPath * indexPath = [NSIndexPath indexPathForRow:indexCell inSection:Sectionindex];            [self.selectArry addObject:indexPath];            SelectCell *cell = (SelectCell*)[self.editTableView cellForRowAtIndexPath:indexPath];            [cell setselectButtonSeleted:cell.selectButton];        }    }}-(void)setselectButtonSeleted:(UIButton *)btn {    if (btn.selected) {        [btn setSelected:NO];    }    [btn setSelected:!btn.selected];}

取消全选

-(void)CancelselectAllCell {  NSLog(@"取消全选");  [self.selectArry removeAllObjects];    // 获取所有的section    NSInteger sectionCount = self.editData.count;    // 获取所有cell的indexpath    for (NSInteger Sectionindex = 0; Sectionindex < sectionCount; Sectionindex++) {        NSString * keyStr = self.editData[Sectionindex];        NSInteger cellCount = [self.sectionData[keyStr] count];        for (NSInteger indexCell = 0; indexCell < cellCount; indexCell++) {            NSIndexPath * indexPath = [NSIndexPath indexPathForRow:indexCell inSection:Sectionindex];            SelectCell *cell = (SelectCell*)[self.editTableView cellForRowAtIndexPath:indexPath];            [cell CancelsetselectButtonSeleted:cell.selectButton];        }    }}-(void)CancelsetselectButtonSeleted:(UIButton *)btn {    if (!btn.selected) {        [btn setSelected:YES];    }    [btn setSelected:!btn.selected];}

请添加洲洲哥的公众号,不定期有干货推送哦,提高装逼技能

洲洲哥的微信公众号

0 0
原创粉丝点击