【发布时间】:2011-09-08 12:07:19
【问题描述】:
我正在动态创建按钮。和创建操作方法
[newButton addTarget:self action:@selector(goToNew:)forControlEvents:UIControlEventTouchUpInside];
我想从 tableview 发送参数(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;<br> UITableViewCell cell = (UITableViewCell)[b superview];int row = [msgTableView indexPathForCell:cell].row;<br> (@"row is :::%d",row);
}
【问题讨论】:
-
我看不懂“但不想使用标签..”
-
在这里看看我的回答:stackoverflow.com/questions/7399119/…
标签: iphone objective-c ios uitableview uibutton