私はfacebookのようなアプリを作成していますので、collectionviewcellで画像の数を変えたいと思っています。私が試してみましたUICollectionViewは一部のセルでラベルと画像を繰り返します
もの:
addSubView:
セルは、複数のオーバーレイが生成され、再利用され、奇妙な結果であるとして、このオプションはなく、動作します。
他のオプションは、ストーリーボードに別のセルを作成していると思いますが、それは約20〜25個のセルが実際には悪い考えにつながります。
私はinitwithframe:オプションを試しましたが、私は単一のセルで異なる数の画像を管理することができません。そうすることはできません。あなたのデバイス上のいくつかの行を表示する場合
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
}
を取り出すことができる方が良いだろうとこれはやり続け、メモリリークを引き起こします。 StoryBoardのcollectionViewCellにUIImageViewとUILabelを追加し、imageViewにアクセスして、imageView.Image = imageと同じものをあなたがすべきラベルで割り当てる必要があります。 – Shashi3456643
コードをもう一度見てください.Jsonに応じてイメージビューの数を変えてください。これは単なる画像ビューではなく、同じ条件の投票オプションがあります。何個の細胞を作りますか?おそらく20または30の意味がない – jovanpreet