day10-自定义删除

2020-06-08  本文已影响0人  js_huh

数据刷新01全局刷新
数据刷新02局部刷新
左滑删除
滑动时自定义多个按钮
单选编辑模式
批量删除


是什么?



//重写创建cell的方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { //在原有cell的基础上,添加自己额外的控件.
        UIImageView * imgTemp = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"check"]];
        imgTemp.hidden = YES;
        [self.contentView addSubview:imgTemp];
        self.checkImageView = imgTemp;
    }
    return self;
}

//设置子控件的位置
- (void)layoutSubviews{
    [super layoutSubviews];
    //设置'勾选图片'的位置
    CGFloat checkWH = 24;
    CGFloat checkX = self.contentView.frame.size.width - checkWH -10;
    CGFloat checkY = self.contentView.frame.size.height * 0.5 - checkWH* 0.5;
    self.checkImageView.frame = CGRectMake(checkX, checkY, checkWH, checkWH);
    
    //调整cell里内容的宽度
    CGRect textFrame = self.textLabel.frame;
    textFrame.size.width = self.textLabel.frame.size.width - checkWH -10;
    self.textLabel.frame = textFrame;
}


//重写wine,给对应的子控件赋值
-(void)setWine:(Wine *)wine{
    _wine = wine;
    self.textLabel.text = wine.name;
    self.imageView.image = [UIImage imageNamed:wine.image];
    self.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",wine.money];\
    
    if (wine.iSCheck) {//显示打钩图片
        self.checkImageView.hidden = NO;
    }else{ //隐藏打钩图片
        self.checkImageView.hidden = YES;
    }
}

- (IBAction)removeBtn{
    NSMutableArray * delArray = [NSMutableArray array];
    NSArray<NSIndexPath *> *indexPaths;
    for (Wine* wineTemp in self.wineData) {
        if (wineTemp.check) {
            [delArray addObject:wineTemp];
        }
    }
    //'数据源'删除数据
    [self.wineData removeObjectsInArray:delArray];
    //刷新数据列表-全局刷新
    [self.tableView reloadData];
}

注意:

原则:


上一篇下一篇

猜你喜欢

热点阅读