我有一个UITableViewCell,包含我从代码生成的特定UIImages。
我遇到了这个问题:第一次加载视图时图像生成正常。但是,例如,如果我向下滚动然后我再回来,我会发现一组完全不同的图像。
我想这是一个与可重复使用的细胞有关的问题,但我真的不知道如何解决它。
for collabsImages in secImageUrls[indexPath.row] {
dispatch_async(dispatch_get_main_queue()){
let image = UIImageView()
image.tag = i
image.userInteractionEnabled = true
image.frame = CGRectMake(CGFloat(10 + i*50 + 7*i), self.view.frame.height/2 - self.navigationController!.navigationBar.frame.height - 55, 40, 40)
image.layer.cornerRadius = image.frame.width/2
image.layer.masksToBounds = true
image.sd_setImageWithURL(NSURL(string: collabsImages), placeholderImage: nil, options: [])
cell1.addSubview(image)
let didTapOnCollabImage = CollabsGestureRecognizer(target: self, action: "didTapOnCollabImage:", row: indexPath.row)
image.addGestureRecognizer(didTapOnCollabImage)
i++
}
}
这是cellForRowAtIndexpath的代码:
let cell1 : cellTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! cellTableViewCell
tableView.allowsSelection = false
cell1.profileImg.userInteractionEnabled = true
let tappedOnImage = UITapGestureRecognizer(target: self, action: "tappedOnImage:")
cell1.profileImg.tag = indexPath.row
cell1.profileImg.addGestureRecognizer(tappedOnImage)
dispatch_async(dispatch_get_main_queue()) {
cell1.profileImg.center = CGPointMake(35, 35)
cell1.usernameLbl.frame = CGRectMake(cell1.profileImg.frame.maxX + 7, cell1.profileImg.frame.minY, self.view.frame.size.width/2, 20)
cell1.usernameLbl.text = (self.jsonFeeds[indexPath.row]["author"] as? String)!
cell1.titleLbl.text = (self.jsonFeeds[indexPath.row]["title"] as? String)
cell1.titleLbl.frame = CGRectMake(cell1.profileImg.frame.maxX + 7, cell1.usernameLbl.frame.maxY + 7, self.view.frame.size.width-20, 20)
let textLabel = UILabel(frame: CGRectMake(cell1.profileImg.frame.minX, cell1.titleLbl.frame.maxY + 20, self.view.frame.width-30, 90))
//textLabel.center = CGPointMake(cell1.center.x, textLabel.center.y)
textLabel.textAlignment = NSTextAlignment.Left
textLabel.numberOfLines = 7
textLabel.text = self.jsonFeeds[indexPath.row]["text"] as? String
textLabel.backgroundColor = UIColor(red: 1/255, green: 109/255, blue: 127/255, alpha: 0.1)
cell1.contentsView.addSubview(textLabel)
let btn = UIButton()
btn.frame = CGRectMake(cell1.frame.maxX - 150, textLabel.frame.maxY + 30, 130, 24)
btn.layer.cornerRadius = 15
btn.layer.borderWidth = 1
btn.setTitle("MESSAGE", forState: UIControlState.Normal)
btn.titleLabel?.font = UIFont(name: "HelveticaNeue-Thin", size: 14)
btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
btn.layer.borderColor = UIColor.blackColor().CGColor
cell1.contentsView.addSubview(btn)
let block: SDWebImageCompletionBlock! = {
(image: UIImage!, error: NSError!, cacheType: SDImageCacheType, imageURL: NSURL!) -> Void in
}
cell1.profileImg.sd_setImageWithURL(NSURL(string: self.imageUrls[indexPath.row]!), completed: block)
}
var i = 0
for collabsImages in secImageUrls[indexPath.row] {
if collabsImages != "This is a post"{
dispatch_async(dispatch_get_main_queue()){
let image = UIImageView()
image.tag = i
image.userInteractionEnabled = true
image.frame = CGRectMake(CGFloat(10 + i*50 + 7*i), self.view.frame.height/2 - self.navigationController!.navigationBar.frame.height - 55, 40, 40)
image.layer.cornerRadius = image.frame.width/2
image.layer.masksToBounds = true
image.sd_setImageWithURL(NSURL(string: collabsImages), placeholderImage: nil, options: [])
cell1.contentsView.addSubview(image)
let didTapOnCollabImage = CollabsGestureRecognizer(target: self, action: "didTapOnCollabImage:", row: indexPath.row)
image.addGestureRecognizer(didTapOnCollabImage)
i++
}
}
}
}
return cell1
答案 0 :(得分:2)
首先,您将调度主线程两次,并在现有调度调用中调度一次。您不需要在cellForRowAtIndexPath中向主线程分派任何内容,除非您正在进行需要返回的后台工作,例如网络提取。
其次,在加载单元格之前,应将可重用单元格上的图像设置为nil。你出细胞后立即image.image = nil
。这可能有助于确保您不会重复使用该图像。
Apple和其他人也有一些关于如何有效地将图像加载到表视图中的非常好的教程。
答案 1 :(得分:0)
使用标签和可重复使用的单元格是一个坏主意。尝试在没有标签帮助的情况下编写相同的代码。将indexpath.row作为标记值会导致出现问题。我认为这会有所帮助