我正在使用XCode的基于导航的应用程序模板创建以UITableView为中心的应用程序。iPhone - 将按钮添加到UITableView中的选定行
当用户在UITableView中选择一行时,我想在该选定单元格内显示一个按钮。我只想在所选单元格中显示此按钮,而不是在其他单元格中显示此按钮。如果用户之后选择了不同的单元,情况也是如此。
我该如何去做这件事?可能吗?
我正在使用XCode的基于导航的应用程序模板创建以UITableView为中心的应用程序。iPhone - 将按钮添加到UITableView中的选定行
当用户在UITableView中选择一行时,我想在该选定单元格内显示一个按钮。我只想在所选单元格中显示此按钮,而不是在其他单元格中显示此按钮。如果用户之后选择了不同的单元,情况也是如此。
我该如何去做这件事?可能吗?
您是否检查了developer.apple.com的UITableViewController和UIButton的文档?
子类UITableViewCell并将按钮添加到它。
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[button setFrame:CGRectMake(320.0 - 90.0, 6.0, 80.0, 30.0)];
[button setTitle:@"Done" forState:UIControlStateNormal];
button.hidden = YES;
[self.contentView addSubview:button];
}
return self;
}
然后覆盖的setSelected像这样:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (lastClickedCell != nil) {
// need to remove button from contentView;
NSArray *subviews = lastClickedCell.contentView.subviews;
for (UIButton *button in subviews) {
[button removeFromSuperview];
}
}
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// this gives you a reference to the cell you wish to change;
UIButton *cellButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // you can change the type to whatever you want
[cellButton setFrame:CGRectMake(x, y, w, h)]; // you will need to set the x,y,w,h values to what you want
// if you want the button to do something, you will need the next line;
[cellButton addTarget:self action:@selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
// now you will need to place the button in your cell;
[cell.contentView addSubview:cellButton];
[tableView reloadData]; // this updates the table view so it shows the button;
lastClickedCell = cell; // keeps track of the cell to remove the button later;
}
编辑:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.button.hidden = !selected;
}
这应该使用类似以下是可能的,当然,你将需要从删除的按钮contentView当你选择一个新的单元格时,所以你需要一点逻辑。子类化可能是一个更简单的解决方案,但这是您不需要子类时需要采用的路线。例如,你可能想在头文件中声明以下内容。
UITableViewCell *lastClickedCell;
然后,你会想把它纳入上面(我将改变放入);
这是一个简单的解决方案!
创建你在像viewDidLoad中某个功能按钮(确保它在.h文件中声明,所以你可以参考它从任何地方)在
- (空)的tableView :(UITableView的*)的tableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath
添加以下内容:
if (yourButton)
[yourButton removeFromSuperview];
[[tableView cellForRowAtIndexPath:indexPath] addSubview:yourButton];
[yourButton setSelected:NO];
[yourButton setHighlighted:NO];
谢谢。将尝试该解决方案。 – steak2002 2011-05-26 18:39:20
我试图让它工作,但不能让按钮显示。你是这个意思吗? - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; \t button = [[UIButton alloc] init]; \t [button setFrame:CGRectMake(20,20,20,20)]; \t [button setTitle:@“Done”forState:UIControlStateNormal]; \t \t self.button.hidden =!选择; \t \t [[self contentView] addSubview:button]; \t \t } 我将rootviewcontroller更改为新的UITableViewCell子类。 – steak2002 2011-05-26 19:56:58
我在示例中增加了一些代码。 – jaminguy 2011-05-26 20:22:02