-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;
}

このコードを実行し、最初の行が選択されている場合はすべて問題ありませんが、2 番目 (mytableview で 2 行のみ) が選択されている場合は、高さが変更された次のすべての空の行も取得します。どこが間違っているの?

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

reloadRowsAtIndexPaths内部で 使用 didSelectRowAtIndexPath

reloadRowsAtIndexPaths でもう一度セルをリロードし didSelectRowAtIndexPathます。

それはうまくいきました、私はデモアプリでクリックされた行を展開しました.:)

于 2015-07-01T12:18:38.517 に答える
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 に答える