美文网首页学无止境控件类
iOS-TableView编辑(多行编辑)

iOS-TableView编辑(多行编辑)

作者: 洲洲哥 | 来源:发表于2016-05-21 17:32 被阅读663次

    本文首发地址

    TableView编辑(多行编辑)

    Demo下载地址- 插入链接
    <a href="https://github.com/7General/HZTableView_selectAll_single" target="_blank"> [ 下载地址 ]

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


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

    洲洲哥的github主页。 —— <a href="https://github.com/7General" target="_blank"> [ 洲洲哥 ]

    项目结构

    image

    系统界面

    image

    编辑界面

    image
    全选功能和取消全选功能可以看一下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];
    }
    

    如有问题可添加我的QQ:1290925041
    还可添加QQ群:234812704(洲洲哥学院)
    欢迎各位一块学习,提高逼格!
    也可以添加洲洲哥的微信公众号
    请添加洲洲哥的公众号,不定期有干货推送哦,提高装逼技能

    洲洲哥的微信公众号

    相关文章

      网友评论

        本文标题:iOS-TableView编辑(多行编辑)

        本文链接:https://www.haomeiwen.com/subject/swhorttx.html