iPhone SDK:TableView中的自定义按钮 [英] iPhone SDK: custom buttons in TableView
问题描述
我在表格"视图中使用自定义按钮,这对我很有效:
I'm using custom buttons in Table view and it works good for me:
- (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.
I'm curious if there is another way of sending a cell identifier to AnotherViewController except setting a tag.
推荐答案
您似乎想让showDetails知道是哪个按钮按下了它.我认为您的操作方式很好,但是如果您希望可以拥有一个功能
It looks like you are want showDetails to know which button pushed it. I think the way you are doing it is fine, but if you wanted you could have a function
- (void)showDetails:(id)sender withObject:(id)object;
,然后使用您的按钮而不是showDetails:(id)sender进行调用;我推断您的对象是您正在执行的操作的字符串,因此可能是
and call that with your button rather than just showDetails:(id)sender; I am inferring that your objects are strings from what you are doing, so it could be
- (void)showDetails:(id)sender withString:(NSString *)string;
但是说实话,您现在的做法很好,为什么要更改?
But honestly the way you are doing it right now is fine, why change?
这篇关于iPhone SDK:TableView中的自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!