2015-12-29 25 views
1

我有一個tableview,可以在選擇單元格時展開,並在再次選擇時摺疊。選擇時,單元格應展開並顯示一個標籤,當您再次選擇時,它會摺疊並隱藏標籤。展開和摺疊工作正常,但如果我擴大單元格後滾動tableview它表現奇怪。一旦它退出視圖並返回,單元格將具有展開的單元格的高度,但應該在展開的單元格中顯示的標籤是隱藏的。如果我再次選擇該單元格,它會摺疊並顯示標籤。我用,UITableviewCell高度在滾動上沒有得到重置

- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [self tableView:t cellForRowAtIndexPath:indexPath]; 
    if([self cellIsSelected:indexPath]) 
     return cell.frame.size.height+35; 
    return cell.frame.size.height; 
} 

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath { 
    // Return whether the cell at the specified index path is selected or not 
    NSNumber *selectedIndex = [self.selectedIndexes objectForKey:indexPath]; 
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue]; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Deselect cell 
    NSLog(@"Select cell:%@",indexPath); 
    [self.tableView deselectRowAtIndexPath:indexPath animated:TRUE]; 

    if([self pickTaskForIndexPath:indexPath].productSpecialMessage){ 
     BOOL isSelected = ![self cellIsSelected:indexPath]; 
     NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected]; 
     [self.selectedIndexes setObject:selectedIndex forKey:indexPath]; 
     PickTaskTableviewCell *cell= [self.tableView cellForRowAtIndexPath:indexPath]; 
     cell.message.hidden=false; 
     cell.messageLabel.text=[self pickTaskForIndexPath:indexPath].productSpecialMessage; 
     cell.messageLabel.lineBreakMode=NSLineBreakByTruncatingTail; 
     cell.messageLabel.numberOfLines=3; 
     if(cell.messageLabel.hidden==true){ 

      cell.messageLabel.hidden = false; 
     } else { 
      cell.messageLabel.hidden = true; 
     } 
     NSLog(@"message:%@",cell.messageLabel.text); 
     [cell layoutIfNeeded]; 
    } 

    self.tableView.rowHeight=UITableViewAutomaticDimension; 
    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 

} 

indexPath被添加到selectedIndexes上didSelectRowAtIndexPath方法 請幫我

+0

如果問題是與標籤,請在選擇(取消選擇)上貼上顯示(隱藏)標籤的代碼。 – danh

+0

添加了didSelectRowAtIndexPath方法 –

回答

1

細胞應僅在cellForRowAtIndexPath配置。當一個狀態改變發生,使一個細胞需要看起來不同,只需重新加載該單元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    PickTaskTableviewCell *cell = (PickTaskTableviewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    // everything else you do to configure the cell goes here, then ... 

    // check the logic here, we want one condition that tells us whether to show the labels 
    if([[self cellIsSelected:indexPath] && self pickTaskForIndexPath:indexPath].productSpecialMessage){ 
     // don't need these here 
     //NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected]; 
     // [self.selectedIndexes setObject:selectedIndex forKey:indexPath]; 
     // PickTaskTableviewCell *cell= [self.tableView cellForRowAtIndexPath:indexPath]; 

     cell.message.hidden=false; 
     cell.messageLabel.text=[self pickTaskForIndexPath:indexPath].productSpecialMessage; 
     cell.messageLabel.lineBreakMode=NSLineBreakByTruncatingTail; 
     cell.messageLabel.numberOfLines=3; 
     cell.messageLabel.hidden=NO; 
    } else { 
     cell.message.hidden=YES; 
     cell.messageLabel.hidden=YES; 
    } 
    NSLog(@"message:%@",cell.messageLabel.text); 
    // don't need this here 
    // [cell layoutIfNeeded]; 
    return cell; 
} 

選擇(可能取消選擇)會導致需要更新的細胞,所以......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // don't deselect it here, just reload it 

    // more on this later... 
    [self.selectedIndexes setObject:selectedIndex forKey:indexPath]; 
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

// probably do the same in didDeselectRowAtIndexPath: 

最後一個(可選)點。有沒有需要維護自己選定的索引路徑列表,UITableView會替你,所以你可以刪除你的selectedIndexes財產,只需要使用表格視圖的方法,例如...

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath { 
    // Return whether the cell at the specified index path is selected or not 
    return [[self.tableView indexPathsForSelectedRows] containsObject:indexPath]; 
} 
相關問題