我正在使用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