我在Table视图中使用自定义按钮,它对我有用:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UIImage *detailsButtonImage;
UIButton *detailsButton;
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//populate cells with array elements
cell.textLabel.text = [itemsArray objectAtIndex:indexPath.row];
//a custom button
detailsButton = [UIButton buttonWithType:UIButtonTypeCustom];
detailsButtonImage = [UIImage imageNamed:@"details.png"];
[detailsButton setBackgroundImage:detailsButtonImage forState:UIControlStateNormal];
detailsButton.frame = CGRectMake(275, 10, 20, 22);
//which cell was tapped?
[detailsButton setTag:[itemsArray objectAtIndex:indexPath.row]];
[detailsButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:detailsButton];
return cell;
}
//showing details of selected item
- (void) showDetails:(id)sender
{
AnotherViewController *anotherViewController = [[AnotherViewController alloc] init];
anotherViewController.title = [sender tag];
anotherViewController.itemDescription = [itemsDescriptions objectAtIndex:[itemsArray indexOfObjectIdenticalTo:[sender tag]]];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
}
我很好奇是否有另一种方法可以将细胞标识符发送到AnotherViewController,除了设置标记。
答案 0 :(得分:1)
看起来你想让showDetails知道哪个按钮推了它。我认为你这样做的方式很好,但如果你想要的话可以有一个功能
并使用您的按钮调用它而不仅仅是showDetails:(id)sender;我推断你的对象是你正在做的字符串,所以它可能是
但老实说,你现在这样做的方式很好,为什么要改变呢?