我需要製作一個UITableView
,其中發佈了不同數量的圖像,例如Facebook
或Twitter
或WeChat
朋友。返回動態細胞高度和排列圖像取決於它們的數量。UITableView imageView重用其他單元格的圖像
我做動態小區高度圖像:
在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;
}
}
現在我遇到的問題,有沒有圖像的一些細胞會加載從別的反覆地方一些圖像,似乎只有無任何後期的圖像單元將遇到這樣的問題
細胞確實有圖像效果好,這取決於包含不同數量的圖像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;
}
所以首先,它與重用imageViewCollection的問題,有什麼建議?讚賞。
你應該設置的ImageView爲nil首先嚐試重用小區之前。基本上把它放在你嘗試使用sd_setImageWithURL下載圖像之前 – Riandy