2016-05-04 61 views
0

我需要製作一個UITableView,其中發佈了不同數量的圖像,例如FacebookTwitterWeChat朋友。返回動態細胞高度和排列圖像取決於它們的數量。UITableView imageView重用其他單元格的圖像

它應該看起來像這樣sample image 1

我做動態小區高度圖像:

HomeTableViewCell.m

使視圖高度自動佈局約束包含後的圖像作爲財產

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height; 

和它的常數設置爲不同的值,以使細胞動力高度

self.height.constant = imageArray.count > 3 ? 260.0 : imageArray.count == 0 ? 0.0 : 130.0; 

,也爲文本做高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
return UITableViewAutomaticDimension; 

}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    return 130 + cell.height.constant; 
} 

完成後,它的工作原理以及同爲第一張圖片,然後我通過SDWebImage框架加載圖像,我創建了imageCollection中的最大6張圖像

@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *imageViewCollection; 

- (void)loadCellDataWithInfo: (SharedUserInfo *)info { 

self.userInfo = info; 
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:info.profileImageUrlStr] placeholderImage:[UIImage imageNamed:@"penguinMe"]]; 

self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.size.width/2.0; 
self.name.text = info.fullName; 
self.username.text = info.userName; 
self.text.text = [info.postDic objectForKey:@"text"]; 
self.likeLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"likesCount"]integerValue]]; 
self.commentLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"commentsCount"]integerValue]]; 
self.elapsed.text = [info.postDic objectForKey:@"elapsed"]; 

NSArray *imageArray = [info.postDic objectForKey:@"images"]; 

[self loadImagesWithURLStr:imageArray]; 

self.height.constant = imageArray.count > 3 ? 260.0 : imageArray.count == 0 ? 0.0 : 130.0; 

} 

- (void)loadImagesWithURLStr: (NSArray *)urlStrArray { 

switch (urlStrArray.count) { 
    case 0: 
     break; 
    case 4: 
     for (int index = 0; index < urlStrArray.count; index++) { 
      if (index >= 2) { 
       [self.imageViewCollection[index + 1] sd_setImageWithURL:[NSURL URLWithString:urlStrArray[index]]]; 
      } else { 
       [self.imageViewCollection[index] sd_setImageWithURL:[NSURL URLWithString:urlStrArray[index]]]; 
      } 
     } 
     break; 

    default: 
     for (int index = 0; index < urlStrArray.count; index++) { 
      [self.imageViewCollection[index] sd_setImageWithURL:[NSURL URLWithString:urlStrArray[index]]]; 
     } 
     break; 
} 
} 

現在我遇到的問題,有沒有圖像的一些細胞會加載從別的反覆地方一些圖像,似乎只有無任何後期的圖像單元將遇到這樣的問題

post sample 2

enter image description here

細胞確實有圖像效果好,這取決於包含不同數量的圖像URL的圖像數組字符串

最後我所有的重用代碼和單元部分代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

[cell loadCellDataWithInfo:self.followingArray[indexPath.row]]; 

return cell; 
} 

- (void)loadCellDataWithInfo: (SharedUserInfo *)info { 

self.userInfo = info; 
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:info.profileImageUrlStr] placeholderImage:[UIImage imageNamed:@"penguinMe"]]; 

self.avatarImageView.layer.cornerRadius = self.avatarImageView.frame.size.width/2.0; 
self.name.text = info.fullName; 
self.username.text = info.userName; 
self.text.text = [info.postDic objectForKey:@"text"]; 
self.likeLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"likesCount"]integerValue]]; 
self.commentLabel.text = [NSString stringWithFormat:@"%li", [[info.postDic objectForKey:@"commentsCount"]integerValue]]; 
self.elapsed.text = [info.postDic objectForKey:@"elapsed"]; 

NSArray *imageArray = [info.postDic objectForKey:@"images"]; 

[self loadImagesWithURLStr:imageArray]; 

self.height.constant = imageArray.count > 3 ? 260.0 : imageArray.count == 0 ? 0.0 : 130.0; 

} 

enter image description here

所以首先,它與重用imageViewCollection的問題,有什麼建議?讚賞。

+0

你應該設置的ImageView爲nil首先嚐試重用小區之前。基本上把它放在你嘗試使用sd_setImageWithURL下載圖像之前 – Riandy

回答

0

您必須使用兩個不同的單元reuseIdentifier。具有圖像的單元(例如cell2)和單元的一個標識符(例如cell1)不具有圖像。所以你必須採取兩個不同的UITableViewCell

代碼將會像下面:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
if(imagesArr.count == 0) 
{ 
cellIdentifier = @"cell1"; 
} 
else{ 
cellIdentifier = @"cell2"; 
} 
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 


[cell loadCellDataWithInfo:self.followingArray[indexPath.row]]; 

    return cell; 
} 
相關問題