iPhone-将按钮添加到UITableView中的选定行 [英] iPhone - Adding button to selected row in UITableView
问题描述
我正在使用XCode的基于导航的应用程序模板来创建以UITableView为中心的应用程序.
I am using XCode's Navigation-based Application template to create an app that centers around an UITableView.
当用户在UITableView中选择一行时,我想在该所选单元格内显示一个按钮.我只想在所选单元格中显示此按钮,而不要在其他任何单元格中显示.如果用户此后选择其他单元格,也会发生同样的情况.
When the user selects a row in the UITableView I want to display a button inside of that selected cell. I want to display this button only in the cell selected and not in any other cells. The same goes if the user selects a different cell afterwards.
我该怎么做?有可能吗?
How do I go about doing this? Is it possible?
推荐答案
子类UITableViewCell并向其中添加按钮.
Subclass UITableViewCell and add the button to it.
- (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,如下所示:
Then override setSelected like so:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
self.button.hidden = !selected;
}
这篇关于iPhone-将按钮添加到UITableView中的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!