首页 > 其他 > 详细

UITableView 自定义cell上面的按钮点击事件

时间:2016-03-23 13:04:49      阅读:249      评论:0      收藏:0      [点我收藏+]

如果需要在控制器中实现按钮的点击事件并且获得对应某行cell的数据,可以用代理的方法,代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HLLocalAccountCell *cell = [HLLocalAccountCell localAccountCellWithTableView:tableView];
    cell.delegate = self;//自定义cell的代理,下面会写到cell的代理
    cell.model = self.accounts[indexPath.row];
    cell.tag = indexPath.row; //传递tag
    return cell;
    
}

在cell.h中:

@protocol CardCellBtnDelegate <NSObject>

- (void)choseCards:(UIButton *)button;

@end
@property (nonatomic,weak)id<CardCellBtnDelegate>delegate;

cell.m:

//cell上的按钮的点击事件
- (void)selBtn:(UIButton *)btn {
    _selBtn.selected = !btn.selected;
    if ([_delegate respondsToSelector:@selector(choseCards:)]) {
        btn.tag = self.tag;
        [_delegate choseCards:btn];
    }
}

然后在控制器的.m文件中执行代理方法:(别忘了继承协议)

#pragma mark - HLLocalAccountCell Delegate
- (void)choseCards:(UIButton *)button {
    NSInteger row1 = button.tag;
    HLLocalAccountModel *model = self.accounts[row1];//获得了model就获得了数据
    NSString *str = [NSString stringWithFormat:@"%li#%@#%@",model.accType,model.accNo,model.accName];
    if (button.selected == YES) {
        [arrM addObject:str];
    }else {
        [arrM removeObject:str];
    }
    NSLog(@"---------%@",arrM);
    //用","拼接数组内的字符串
    NSString *str1 = [arrM componentsJoinedByString:@","];
    NSLog(@"==========%@",str1);
    mulParams = [NSMutableDictionary dictionaryWithDictionary:self.params];
    [mulParams setValue:str1 forKey:@"account_info"];
    
}

先写这么多,以后继续补充

 

UITableView 自定义cell上面的按钮点击事件

原文:http://www.cnblogs.com/zpt1011/p/5310479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!