我正在创建像facebook这样的应用程序,所以有一个问题,我想在collectionviewcell上有不同数量的图像,而且我还有一个轮询选项,因此不会产生任何民意调查。
我尝试过的事情:
addSubView:
此选项有效,但在重复使用单元格时会生成多个叠加层并产生奇怪的结果。
我想到的其他选项是在故事板中创建不同的单元格,但这将导致大约20到25个单元格真的很糟糕。
然后我尝试了initwithframe:option但我无法在单个单元格上管理不同数量的图像。所以不可能。
这是我目前的代码:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! FirstCollectionViewCell
// Configure the cell
if feed[indexPath.row]["images"].arrayValue.count == 1 {
let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
var image1:UIImageView
image1 = UIImageView(frame:CGRectMake(0, 56, self.view.frame.size.width, self.view.frame.size.width*0.65))
if feed[indexPath.row]["images"][0]["mediaType"].intValue == 1 {
image1.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][0]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][0]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image1.image = image
})
})
}
image1.tag = 0
image1.userInteractionEnabled = true
image1.clipsToBounds = true
image1.addGestureRecognizer(tapped)
image1.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image1)
}else if feed[indexPath.row]["images"].arrayValue.count == 2 {
let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
let tapped2:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped2.numberOfTapsRequired = 1
var image1:UIImageView
image1 = UIImageView(frame:CGRectMake(0, 56, self.view.frame.size.width/2-2, self.view.frame.size.width*0.45))
if feed[indexPath.row]["images"][0]["mediaType"].intValue == 1 {
image1.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][0]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][0]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image1.image = image
})
})
}
image1.tag = 0
image1.userInteractionEnabled = true
image1.clipsToBounds = true
image1.addGestureRecognizer(tapped)
image1.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image1)
var image2:UIImageView
image2 = UIImageView(frame:CGRectMake(self.view.frame.size.width/2+2, 56, self.view.frame.size.width/2-2, self.view.frame.size.width*0.45))
if feed[indexPath.row]["images"][1]["mediaType"].intValue == 1 {
image2.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][1]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][1]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image2.image = image
})
})
}
image2.tag = 1
image2.userInteractionEnabled = true
image2.clipsToBounds = true
image2.addGestureRecognizer(tapped2)
image2.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image2)
}else if feed[indexPath.row]["images"].arrayValue.count == 3 {
let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
let tapped2:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped2.numberOfTapsRequired = 1
let tapped3:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped3.numberOfTapsRequired = 1
var image1:UIImageView
image1 = UIImageView(frame:CGRectMake(0, 56, self.view.frame.size.width/2-2, (self.view.frame.size.width*0.45)/2 - 2))
if feed[indexPath.row]["images"][0]["mediaType"].intValue == 1 {
image1.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][0]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][0]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image1.image = image
})
})
}
image1.tag = 0
image1.userInteractionEnabled = true
image1.clipsToBounds = true
image1.addGestureRecognizer(tapped)
image1.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image1)
var image2:UIImageView
image2 = UIImageView(frame:CGRectMake(self.view.frame.size.width/2+2, 56, self.view.frame.size.width/2-2, self.view.frame.size.width*0.45))
if feed[indexPath.row]["images"][1]["mediaType"].intValue == 1 {
image2.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][1]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][1]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image2.image = image
})
})
}
image2.tag = 1
image2.userInteractionEnabled = true
image2.clipsToBounds = true
image2.addGestureRecognizer(tapped2)
image2.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image2)
var image3:UIImageView
image3 = UIImageView(frame:CGRectMake(0, 56+(self.view.frame.size.width*0.45)/2 + 2, self.view.frame.size.width/2-2, (self.view.frame.size.width*0.45)/2 - 2))
if feed[indexPath.row]["images"][2]["mediaType"].intValue == 1 {
image3.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][2]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][2]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image3.image = image
})
})
}
image3.tag = 2
image3.userInteractionEnabled = true
image3.clipsToBounds = true
image3.addGestureRecognizer(tapped3)
image3.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image3)
}else if feed[indexPath.row]["images"].arrayValue.count == 4 {
let tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
let tapped2:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped2.numberOfTapsRequired = 1
let tapped3:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped3.numberOfTapsRequired = 1
let tapped4:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped4.numberOfTapsRequired = 1
var image1:UIImageView
image1 = UIImageView(frame:CGRectMake(0, 56, self.view.frame.size.width/2-2, (self.view.frame.size.width*0.45)/2 - 2))
if feed[indexPath.row]["images"][0]["mediaType"].intValue == 1 {
image1.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][0]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][0]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image1.image = image
})
})
}
image1.tag = 0
image1.userInteractionEnabled = true
image1.clipsToBounds = true
image1.addGestureRecognizer(tapped)
image1.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image1)
var image2:UIImageView
image2 = UIImageView(frame:CGRectMake(self.view.frame.size.width/2+2, 56, self.view.frame.size.width/2-2, (self.view.frame.size.width*0.45)/2 - 2))
if feed[indexPath.row]["images"][1]["mediaType"].intValue == 1 {
image2.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][1]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][1]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image2.image = image
})
})
}
image2.tag = 1
image2.userInteractionEnabled = true
image2.clipsToBounds = true
image2.addGestureRecognizer(tapped2)
image2.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image2)
var image3:UIImageView
image3 = UIImageView(frame:CGRectMake(0, 56+(self.view.frame.size.width*0.45)/2 + 2, self.view.frame.size.width/2-2, (self.view.frame.size.width*0.45)/2 - 2))
if feed[indexPath.row]["images"][2]["mediaType"].intValue == 1 {
image3.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][2]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][2]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image3.image = image
})
})
}
image3.tag = 2
image3.userInteractionEnabled = true
image3.clipsToBounds = true
image3.addGestureRecognizer(tapped3)
image3.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image3)
var image4:UIImageView
image4 = UIImageView(frame:CGRectMake(self.view.frame.size.width/2+2, 56+(self.view.frame.size.width*0.45)/2 + 2, self.view.frame.size.width/2-2, (self.view.frame.size.width*0.45)/2 - 2))
if feed[indexPath.row]["images"][1]["mediaType"].intValue == 1 {
image4.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["images"][1]["imageURL"].stringValue), placeholderImage: UIImage(named: "placeholder"))
}else{
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
let image = self.drawImage(UIImage(named: "play_button")!, inImage: self.thumbnailImageFromURL(NSURL(string: self.feed[indexPath.row]["images"][3]["imageURL"].stringValue)!))
dispatch_async(dispatch_get_main_queue(), { () -> Void in
image4.image = image
})
})
}
image4.tag = 3
image4.userInteractionEnabled = true
image4.clipsToBounds = true
image4.addGestureRecognizer(tapped4)
image4.contentMode = UIViewContentMode.ScaleAspectFill
cell.addSubview(image4)
}
cell.profile.sd_setImageWithURL(NSURL(string: feed[indexPath.row]["profilePic"].stringValue), placeholderImage: UIImage(named: "profileplaceholder"))
cell.name.text = feed[indexPath.row]["name"].stringValue
cell.time.text = feed[indexPath.row]["postedTime"].stringValue
if feed[indexPath.row]["totalFlowers"].stringValue == "" {
cell.flowerLabel.text = "0 flowers"
}else{
cell.flowerLabel.text = "\(feed[indexPath.row]["totalFlowers"].intValue) flowers"
}
if feed[indexPath.row]["totalHelpful"].stringValue == "" {
cell.helpfulLabel.text = "0 Helpful"
}else{
cell.helpfulLabel.text = "\(feed[indexPath.row]["totalHelpful"].intValue) helpful"
}
if feed[indexPath.row]["totalComments"].stringValue == "" {
cell.commentLabel.text = "0 Comments"
}else{
cell.commentLabel.text = "\(feed[indexPath.row]["totalComments"].intValue) Comments"
}
cell.flower.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
return cell
}
答案 0 :(得分:1)
当您在设备上显示某些行时,这些行将填充数据。想象一下你的图像是A / B / C等。
Row1 A - -
Row2 A B -
Row3 - B C
etc
滚动时,计算并绘制单元格的新值。 ViewController根据以前的值计算这些行。
在第1行的旧情况下,仅显示了图像A. 您只想显示图像C.所以您将C添加到单元格。 视图控制器将采用行A,并添加图像C,结果为:
Row1 A - C // Both images A and B displayed!
解决方案很简单。您必须在添加图像之前清除图像!
答案 1 :(得分:1)
这是因为玩具dequeueReusableCellWithReuseIdentifier
它是正确的方式,但iOS重新创建时会在滚动时创建单元格,这意味着您需要在设置新内容之前清除内容。
如果在collectionView willDisplay Cell
中使用此委托方法可以检索已创建的单元格,那会更好