我正在動態創建按鈕。並通過在不使用標籤的情況下在按鈕操作方法中傳遞參數
[newButton addTarget:self action:@selector(goToNew:)forControlEvents:UIControlEventTouchUpInside];
我想給從實現代碼如下參數(indexpath.row),但不希望使用標籤..因爲我需要這個標籤將保持相同的所有按鈕,如何傳遞的創建操作方法按鈕操作中的參數?
實際上,我在每個tableview單元格中添加按鈕,並且我想爲所有這些按鈕採取行動,但是如果我使用tag = indexpath.row並將其與動作一起使用,它將起作用,但疊加按鈕的問題會發生。希望標籤不變。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UIButton *btn;
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(goToNew:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 55;
[cell.contentView addSubview:btn];
}
else {
btn = (id)[cell.contentView viewWithTag:55];
}
return cell;
}
- (void) goToNew:(id)sender
{
UIButton *b = (UIButton *)sender;
的UITableViewCell 細胞=(的UITableViewCell)並[b上海華];
int row = [msgTableView indexPathForCell:cell].row;
(@「row is :::%d」,row);
}
我不明白「但不想用標籤..」 – Maulik
是的,我沒有....... – PJR
看看我的答案在這裏:http://stackoverflow.com/questions/7399119/custom-uitableviewcell-button-action/14955341#14955341 – djskinner