2013-08-21 73 views
1

我有一個使用分鏡,看起來就像實現了一個UITableViewCell重新排列細胞子視圖時的UITableViewCell調整大小

enter image description here

這裏是電池應該是什麼樣子,不帶圖像:

enter image description here

我一直在擺弄着這些限制,一直在試圖弄清楚這個問題,但沒有運氣。我對約束有很好的理解,以及如何以編程方式添加它們,但對這個特定問題沒有任何好運,並且覺得我只是在沒有邏輯思維過程的情況下爲單元格添加布局約束。單元格代表新聞源帖子,在頂部的主圖像視圖中可能有也可能沒有圖像,並且應該具有以下行爲。如果單元格中沒有圖像,則帶有類似和註釋計數的底​​部欄向上移動以與單元格的頂部對齊。我通過設置一個約束來保持較小的圖片視圖,帖子標題,發佈時間和帖子內容距離單元格底部一定距離,從而實現了此行爲。這種方法有效,當單元格在heightForRowAtIndexPath方法中調整大小時,子視圖會適當移動。問題出現在帖子內容中的文字大於一行時。單元格的高度調整正確,但文本視圖的頂部停留在相同的位置並向下增長並溢出到下一個單元格中。當我放置約束來將四個子視圖與單元格的頂部對齊時,我會在沒有圖像時發生問題,並且帖子內容大於一行。在這種情況下,單元格的大小調整爲小於其原始大小,子視圖停留在約束指定的距離處。較小的圖像,文章標題,時間和內容被剪切並且不顯示。在這麼多不同的情況下,這是一個奇怪的問題。我在這工作了近兩天,真的可以用別人的想法來解決這個問題。我希望這不是太混亂,謝謝你的幫助!

+0

小方形圖像視圖應該在哪裏,並移動?它是否應該重疊較大的圖像視圖?它總是有圖像,或者它有時也是空白的? – rdelmar

+0

另外,當大圖像視圖中沒有圖像時,您希望底部欄位於其他內容之上?在單元格的頂部? – rdelmar

+0

我編輯了這篇文章,並在沒有圖像的情況下添加了單元格應該看起來像的圖像。 @rdelmar – ScottOBot

回答

0

嘿@rdelmar感謝您的解決方案!最後,我最終只是在故事板文件中設計了兩個不同的單元格,它們具有不同的重用標識符,但是具有相同的子類。然後,我檢查了cellForRowAtIndexPath方法,如果該單元格是否有內容,並且分配了正確的標識符。如果這是不正確的做法,或者會導致問題,請在評論中告訴我。

1

我有辦法解決這個問題,但我相信還有很多其他的方法。我給了兩個圖像視圖固定的高度約束。小圖像視圖和頂部標籤(帖子標題)具有固定高度到單元格的頂部 - 這些以及大圖像視圖的高度約束均具有IBOutlet,因此可以在代碼中對其進行更改。底部標籤(Post Content)將其行數設置爲0,並且其高度約束(所有標籤具有標準21點高度開始)都有一個IBOutlet。在代碼中,我檢查每個indexPath中是否存在圖像,並相應地更改約束。

- (void)viewDidLoad { 
    UIImage *image1 = [UIImage imageNamed:@"House.tiff"]; 
    [super viewDidLoad]; 
    self.theData = @[@{@"pic":image1, @"post":@"short post"},@{@"post":@"short post"},@{@"pic":image1, @"post":@"Long long post with some extra stuff, and even some more"},@{@"post":@"Long long post with some extra stuff, and even some more"}]; 
    [self.tableView reloadData]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.theData.count; 
} 

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CGFloat ivHeight = (self.theData[indexPath.row][@"pic"])? 215 : 0; // 215 is the fixed height of the large image view 
    CGSize labelSize = [self.theData[indexPath.row][@"post"] sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(152, CGFLOAT_MAX)]; 
    return 140 + ivHeight + labelSize.height; // the 140 was determined empirically to get the right spacing between the 3 labels and the bottom bar 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    cell.label.text = self.theData[indexPath.row][@"post"]; 
    cell.iv.image = self.theData[indexPath.row][@"pic"]; 
    if(self.theData[indexPath.row][@"pic"] == nil){ 
     cell.heightCon.constant = 0; // heightCon is the outlet to the large image view's height constraint 
     cell.ivTopCon.constant = 8; // ivTopCon is the outlet to the small image view's spacing to the top of the cell 
     cell.labelTopCon.constant = 8; // labelTopCon is the outlet to thetop label's spacing to the top of the cell 
    }else{ 
     cell.heightCon.constant = 215; // this number and the following 2 are taken from the values in IB 
     cell.ivTopCon.constant = 185; 
     cell.labelTopCon.constant = 233; 
    } 
    CGSize labelSize = [self.theData[indexPath.row][@"post"] sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(152, CGFLOAT_MAX)]; 
    cell.labelHeightCon.constant = labelSize.height; 
    return cell; 
} 
相關問題