-1

我需要动态更改单元格的行。这就是为什么我使用 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 中只有两行),我什至会得到修改后高度的所有下一个空行。我哪里错了?

4

3 回答 3

0

对于动态更新单元格的高度,您可以使用以下代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

      if (indexPath.row == 1)
          selectedIndex = 150;
      else
          selectedIndex = 44;

      [tbl_news_list reloadData];
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return selectedIndex;
}
于 2014-01-16T11:12:05.213 回答
0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
       return 44;
    }
    else if (indexPath.row == 1) {
       return 88;
    }
    else{
       return 0;
    }
}
于 2015-06-30T16:03:07.200 回答
0

reloadRowsAtIndexPaths在里面 使用 didSelectRowAtIndexPath

reloadRowsAtIndexPaths 将再次重新加载单元格 didSelectRowAtIndexPath

它工作得很好,我在演示应用程序中扩展了单击的行。:)

于 2015-07-01T12:18:38.517 回答