美文网首页
UITableViewCell中按钮点击

UITableViewCell中按钮点击

作者: younger_times | 来源:发表于2017-07-21 13:28 被阅读1207次
QQ20170721-130538.jpg

达到的效果

如图所示,当前页面展示的是“已收藏”的页面,如果我再次点击“已收藏”,就不在关注,然后动画效果移除。

出现的问题

很明显,即使UITableViewDelegate的点击事件也是发生在cell上面,UIButton事件是不会响应的。UIButto也是在celll里面,如果要做动画移除效果,得知道indexPath的位置,有人说,tag能进行在创建每一个Button时,设置tag,但如果cell被缓存池后tag不会改变的话,点击会发生混乱。

出现了错误

最初想的是,使用事件传递响应链,UIButton之下是Cell,想继续传递事件下去,但没有成功,即使我获得了cell,貌似不会响应。

解决方案 —— Cell初始化重写和协议

只粘贴代码的核心部分

//实现协议,当UIButton按钮被点击
@protocol CollectMasterProtocol <NSObject>

/**
 按钮点击协议

 @param btn 被点击的按钮
 @param label 这个是为了设置“未收藏”的label 没多大用处
 @param cell 传递cell
 @param index indexPath
 */
@required
-(void)buttonClick:(UIButton *)btn CollectStatusLabel:(UILabel *)label Cell:(UITableViewCell *)cell Index:(NSIndexPath*)index;
@end

@interface YKCollectMasterTableViewCell : UITableViewCell
/**
 收藏大师model
 */
@property(nonatomic,strong)YKCollectMasterModel *collectMasterModel;
/**
 代理
 */
@property(weak,nonatomic)id<CollectMasterProtocol>collectMasterDelegate;

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier Index:(NSIndexPath*)index;
@end

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier Index:(NSIndexPath *)index{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
         //todo  初始化UI等操作省略

        //@property(nonatomic,assign)NSIndexPath *index; 放在m文件里,也是主要的
        self.index = index;
        //"收藏"按钮点击
        UIButton *collectStatusBtn  = [[UIButton alloc]init];
        [collectStatusBtn setImage:[UIImage imageNamed:@"collect"] forState:UIControlStateNormal];
        [collectStatusBtn addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
        self.collectStatusBtn       = collectStatusBtn;
        [self.contentView addSubview:self.collectStatusBtn];
         //todo  初始化UI等操作省略        
    }
    return self;
    
}

-(void)Click{
    if ([self.collectMasterDelegate respondsToSelector:@selector(buttonClick:CollectStatusLabel:Cell:Index:)]) {
        [self.collectMasterDelegate buttonClick:self.collectStatusBtn CollectStatusLabel:self.collectStatusLabel Cell:self Index:self.index];
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    static NSString *ssid = @"YKCollectMasterTableViewCell";
    
    YKCollectMasterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ssid];
    if (cell==nil) {
        
        cell = [[YKCollectMasterTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ssid Index:indexPath];
        
        //cell中的协议
        cell.collectMasterDelegate = self;
        [cell setCollectMasterModel:self.collectMasterData[indexPath.row]];
    }
    return cell;
}
#pragma mark --CollectMasterProtocol
-(void)buttonClick:(UIButton *)btn CollectStatusLabel:(UILabel *)label Cell:(UITableViewCell *)cell Index:(NSIndexPath *)index{

    //移除
    [self.collectMasterData removeObjectAtIndex:index.row];
    
    //移除动画
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
    [btn setImage:[UIImage imageNamed:@"unCollect"] forState:UIControlStateNormal];
    [label setText:@"取消收藏"];
    
}

不会和didSelectRowAtIndexPath相冲突

因为是在项目中直接使用的没有demo,如果需要demo我后面补上

相关文章

网友评论

      本文标题:UITableViewCell中按钮点击

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