I am using XCode's Navigation-based Application template to create an app that centers around an UITableView.
When the user selects a row in the UITableView I want to display a button inside o开发者_如何学运维f 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?
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;
}
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;
}
This should be possible using something like the following:
- (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;
}
EDIT: you will of course need to remove the button from the contentView when you select a new cell, so you will need a little logic for that. Subclassing may be an easier solution, but this is the route you will need to take if you don't want to sublass. For example, you would want to declare the following in your header.
UITableViewCell *lastClickedCell;
Then you will want to incorporate that into the above (which I'll change to put it in);
Did you check developer.apple.com for the documentation of the UITableViewController and the UIButton ?
THIS IS A SIMPLE SOLUTION!
Create your button in somewhere like the viewDidLoad function (make sure it is declared in the .h file so you can refer to it from anywhere)
in the -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
add the following:
if (yourButton)
[yourButton removeFromSuperview];
[[tableView cellForRowAtIndexPath:indexPath] addSubview:yourButton];
[yourButton setSelected:NO];
[yourButton setHighlighted:NO];
精彩评论