2014-01-16 33 views
-1

我需要動態更改單元格的行。這就是爲什麼我使用的selectedIndex變量來存儲選定行heightForRowAtIndexPath涉及很多單元格

- (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中只有兩行)被選中,我甚至會得到修改高度的所有下一個空行。我哪裏錯了?

+0

heightForRowAtIndexPath不叫每個用戶選擇行時間。 –

+1

@BijoyThangaraj OP在用戶選擇一行時重新加載UITableView – Tim

+0

selectedIndex定義在哪裏?你不需要導致返回44的兩個條件,只有一個。 – Tim

回答

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

如果indexPath.row = 1,則此代碼爲所有行創建更大的高度(兩行填充,其他空)。如果indexPath.row = 0,則所有行都爲正常高度。 – cicaletto79

+0

您可以根據您的要求分配selectedIndex值我有隻是給你的想法。 –

+0

你做完了嗎? –

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.row == 0) { 
     return 44; 
    } 
    else if (indexPath.row == 1) { 
     return 88; 
    } 
    else{ 
     return 0; 
    } 
} 
0

使用reloadRowsAtIndexPathsdidSelectRowAtIndexPath

reloadRowsAtIndexPaths將再次重新加載didSelectRowAtIndexPath細胞。

它工作得很好,我擴大點擊行中的演示應用程序。:)