2016-01-14 58 views
0

我有一個單元格,其中包含2個多行標籤和一個UIImageView。單元具有動態高度,並使用Autolay自動計算。 我的問題是,我在cellForRowAtIndexPath中下載圖像:直到那時我不知道圖像的高度。在開始的單元格沒有正確的佈局,我不得不滾動到其他單元格,當我回到我的單元格(它重新繪製),然後佈局是正確的。圖像下載時如何重新繪製單元格? 下面是我正在使用的代碼:表格單元格內的圖像塊和更改自動佈局

NSString static * cellIdentifier = @"titlePostCell"; 

PostTitleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
    cell = [[PostTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 

cell.titleLabel.text = self.entry.title; 
cell.descriptionLabel.text = self.entry.entry_description[@"value"]; 

[cell setNeedsUpdateConstraints]; 


[cell.postImageView pin_setImageFromURL:[[CTImageHelper sharedInstance] resizedImageURLConverterFromStringWithPrefix:self.entry.card_image] completion:^(PINRemoteImageManagerResult *result) { 

    cell.heightSize = [NSNumber numberWithInt:result.image.size.height/2]; 
    cell.heightImageConstraint.constant = cell.heightSize.floatValue; 

}]; 

return cell; 

謝謝!

+0

您需要使單元格自動佈局,使單元格的高度應該具有uiimageview高度的相關性。我不認爲在塊內的cellForRowIndex中給出約束是好方法。 – nikhil84

+0

@ nikhil84單元取決於圖像大小和兩個標籤,因爲它們是多行,並且它們沒有固定大小 –

回答

0

如果你有一個指針到單元(例如,在你的塊),你可以通過下面的代碼實現這一點:

NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
[tableView reloadRowsAtIndexPaths:@[indexPath]]; 

但是,我認爲,在一般情況下,調整表視圖細胞無用戶特別要求你這樣做是一個糟糕的用戶界面的做法。想象一下,當用戶嘗試使用它時,用戶界面會跳躍!相反,我會在你的細胞中使用固定大小的UIImageView。您可以將任意大小的圖像設置爲contentModeUIViewContentModeScaleAspectFit,以放入此圖像視圖。

相關問題