2013-01-08 82 views
0

我有一個表格,其中當用戶點擊一個單元格時,細節單元格下降到它的正下方。我已經想出瞭如何使用insertRowAtIndexPath來實現這一點,但是細節單元的行高比正常單元高。我試過heightForRowAtIndexPath,但似乎只是在初始加載視圖時調用(或使用reloadData)。但是,我不希望將細節單元格添加到表格視圖的數據中,因此可以將reloadData消除,因此可以刪除heightForRowAtIndexPath。我有一個自定義UITableViewCell分類爲ItemDetailCell。我認爲這可能會從故事板中的關聯單元格加載指定的行高,但似乎不起作用。這是我在cellForRowAtIndexPath怎樣努力:用insertRowAtIndexPath調整UITableViewCell高度

if (indexPath.row == _detailCellIndexPath.row && _detailCellIndexPath) { 
    NSLog(@"detailCell"); 

    ItemDetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier forIndexPath:_detailCellIndexPath]; 

    //customize cell 

    return detailCell; 
} 
else { 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    //customize cell 

    return cell; 
} 

我得到在控制檯中NSLog(@"detailCell")的回報,所以我知道它獲取調用,但它仍然不調整單元格高度。

任何幫助,非常感謝!提前致謝。

回答

1

heightForRowAtIndexPath也被稱爲後

[self.tableView beginUpdates]; 
[self.tableView endUpdates]; 

是否有任何理由,你爲什麼不想細胞添加到您的收藏後盾?最簡單的解決方案是將一個特定的項目添加到您的表格視圖支持數組中,然後在beginUpdatesendUpdates區塊內調用insertRowAtIndexPath。這將觸發標準委託/數據源表視圖方法(包括heightForRowAtIndexPath),因此您可以根據具體的UITableViewCell類(ItemDetailCellCustomCell)返回不同的大小。

+0

由於詳細信息單元是我在Master-Detail模板中詳細信息視圖的版本,因此它只是臨時的,並不是表格中的永久添加項,因此我不希望它在備份數據中,因爲我必須不斷移除它。如果可以的話,這是我想避免的。 –

+1

@MusicMathTech reloadData動畫效果不佳。如果用戶有辦法顯式插入/刪除行(例如通過點擊單元格),推薦的方法是將插入/刪除代碼放入beginUpdates endUpdates動畫塊中。 –

相關問題