如果需要在控制器中实现按钮的点击事件并且获得对应某行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"]; }
先写这么多,以后继续补充
原文:http://www.cnblogs.com/zpt1011/p/5310479.html