在我的应用程序中,我有一个UITableView,我有两个不同的自定义UITableViewCells.UITableView最初加载了一种自定义UITableViewCell.当我在UITableView中选择其中一个单元格时,我想更改使用其他类型的自定义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;
}
Run Code Online (Sandbox Code Playgroud)
在您的didSelectRow...方法中,您需要更新一个标志(存储在一个实例变量中),指示单元格的新"模式".然后通过调用tableView reloadRowsAtIndexPaths:withRowAnimation:传入选定的indexPath 重新加载该行.
在您的cellForRow...方法中,使用该标志来确定要用于行的两种类型的单元格中的哪一种.