2014-02-19 40 views
1

我有自定義UITableViewCell自定義的UITableViewCell內容查看高度

@interface PostCell() 

@property (weak, nonatomic) IBOutlet PostView *postView; 

@end 

@implementation PostCell 

- (void)setPost:(Post *)newPost 
{ 
    self.postView.post = newPost; 
} 

@end 

其中包含自定義PostViewUIView子類)。

這是我送PostmodelPostCell然後轉發該取決於Postmodel內容modelPostViewsubviewPostViewsubview繪製本身:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"PostCell"; 

    PostCell *postCell = (PostCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    Post *post = self.displayPosts[indexPath.row]; 
    [postCell setPost:post]; 

    return postCell; 
} 

我希望這是一個好辦法,我引用上一些蘋果自定義的UITableView示例用於快速繪製。

可以這麼說Postmodeltitleimage屬性。由於每個PostView將不同height我怎麼能計算出height並返回到heightForRowAtIndexPath

難道是好到我的自定義UITableView細胞添加這個方法:

- (CGFloat)heightForCell 
{ 
    return self.postView.frame.size.height; 
} 

然後

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // here we will need custom height based on image and text 
    PostCell *postCell = (PostCell *)[tableView cellForRowAtIndexPath:indexPath]; 
    return [postCell heightForCell]; 
} 

我都試過,但

PostCell *postCell = (PostCell *)[tableView cellForRowAtIndexPath:indexPath]; 

拋出了我的異常。

首先我想知道它的好方法,如果是的話,我可以繼續去除異常中斷。

回答

2

當你調用- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath時,你的單元格還沒有被創建(這就是爲什麼你會得到這個異常)。最好的方法是將新屬性添加到Post對象中,調用它cellHeight,並將單元格的高度保存在此對象中。然後返回像post.cellHeight

+0

哦,所以之前調用了heightForRow?那麼這是有道理的:D –

+0

@ZeljaHuber yep :) –

相關問題