我需要动态更改单元格的行。这就是为什么我使用 selectedIndex 变量来存储选定的行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
TarModel * tar = self.tarList[indexPath.row];
if (selectedIndex == indexPath.row) {
static NSString *CellIdentifier = @"detailCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
else {
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
}
// Configure the cell...
cell.textLabel.text = tar.way;
cell.detailTextLabel.text = [NSString stringWithFormat:@"cost: € %@",
tar.price];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"indexpath is %d", indexPath.row);
selectedIndex = indexPath.row;
isSearching = YES;
[self.tableView reloadData];
}
然后我用这个改变我选择的行的高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat height;
if(selectedIndex == -1) {
height = 44.0;
}
else if (selectedIndex == indexPath.row) {
height = 88.0;
}
else {
height = 44.0;
}
return height;
}
当我运行此代码并选择第一行时,一切正常,但如果选择第二行(mytableview 中只有两行),我什至会得到修改后高度的所有下一个空行。我哪里错了?