选择时更改UITableViewCell
问题描述:
在我的应用程序中,我有一个UITableView,我有两个不同的自定义UITableViewCells。 UITableView最初加载了一种自定义的UITableViewCell。当我的UITableView中选择其中一个单元格时,我想更改使用其他类型的自定义UITableViewCell选择的单元格。这可能吗?让我听听你有什么。选择时更改UITableViewCell
感谢, NSNolan
#pragma mark - UITableView delegate methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.array count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == [self currentCellIndex]) {
[self setCurrentCellIndex:-1];
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil];
OldCell *cell = (OldCell *)[tableView cellForRowAtIndexPath:indexPath];
cell = [nibs objectAtIndex:0];
}
else {
[self setCurrentCellIndex:indexPath.row];
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil];
NewCell *cell = (NewCell *)[tableView cellForRowAtIndexPath:indexPath];
cell = [nibs objectAtIndex:0];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"OldCell";
OldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"OldCell" owner:self options:nil];
cell = [nibs objectAtIndex:0];
}
return cell;
}
答
在你didSelectRow...
方法,你需要更新表示小区的新的“模式”的标志(存储在一个实例变量)。然后通过调用tableView reloadRowsAtIndexPaths:withRowAnimation:
传入选定的indexPath来重新加载该行。
在您的cellForRow...
方法中,使用标志来确定用于该行的两种类型的单元格。
+0
谢谢@rmaddy。完美的作品。 – NSNolan 2013-03-09 05:22:17
有特别的代码块给你带来麻烦吗? http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html#//apple_ref/doc/uid/TP40007451 – 2013-03-09 04:37:06
我不遵循你所要求的或者链接的相关性。 – NSNolan 2013-03-09 04:38:42
您可以通过保存当前所选indexPath的实例变量来实现此目的。然后重新加载tableview。在cellForRowAtIndexPath中:您可以检查indexPath是否等于selectedIndexPath,然后初始化第二个自定义单元格。 – Anupdas 2013-03-09 04:42:52