2011-07-17 65 views
0

我有我的UITableView自定義單元格。我在我的手機上放了一個左右滑動的觀察者。其中如果用戶在單元格上滑動,它將調用一個函數。這個函數基本上創建了需要添加到單元格中的UIView。自定義單元格和UITableViewController建議

我想我的代碼使用MVC最佳實踐。所以我認爲最合適的做法是將UIView傳遞給自定義單元格,並讓我的自定義單元格實現將其添加到單元格中。在我的自定義單元格中,我也有UIView的屬性。

問題是我還需要調整單元的高度。現在,我有方法是:

+ (CGFloat)tableView:(UITableView *)tableView rowHeightForObject:(id)item { 

} 

這是不是索引路徑它的對象類似於常規heightForRowAtIndexPath。在這個方法中,我需要確定是否存在需要添加的UIView。如果有,高度需要根據這個進行調整。我似乎無法解決這個問題。在這種方法中,我不能做self.optionsView,或訪問我的自定義單元格子類中的任何屬性。那麼如何檢查選項是否被添加?

回答

0

您可以將rowHigh屬性添加到您的自定義單元類,並將另一個高度var添加到您的表視圖數據源數據。 每次更改自定義單元格時,請確保設置height屬性,並將其分配給正確位置的數據源數據。

然後,在裏面heightForRowAtIndexPath只是要求身高的數據。

希望很明顯。

0

正如Idan指出的那樣,向單元索取內容大小是很好的做法。我只想用標準heightForRowAtIndexPath方法是這樣的:

//Assuming myCustomCell contains a placeholder for my additional view called innerView. 
//If the place holder is nil I don't need to adjust cells height. 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    //Get the cellPath and myCustomCell at this path 
    NSIndexPath *rowPath = [NSIndexPath indexPathForRow:row inSection:0]; 
    MyCustomTableCell *myCustomCell = (MyCustomTableCell*)[self.table cellForRowAtIndexPath:rowPath]; 

    if(myCustomCell.innerView != nil) 
     return myCustomCell.innerView.frame.size.height + someDefaultCellHeight; 

    else 
     return defaultCellHeight; 
} 
+0

,因爲我用rowHeightForObject這是不行的,這裏有沒有indexPath – adit

+0

那麼你不能將數據添加到對象,像USER_INFO字典或喜歡? – Idan