【发布时间】:2017-09-22 10:25:02
【问题描述】:
如何使用多个withReuseIdentifier?
这是代码 因为我有 4 个按钮不起作用 当我使用标识符“1”时,其他按钮不起作用
这是代码
extension ViewController : UICollectionViewDataSource
{
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return interests3.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as! interestCollectionViewCell
cell.interest2 = self.interests3[indexPath.item]
return cell
}
新代码
import UIKit
class interestCollectionViewCell: UICollectionViewCell
{
var interest2: Interest1! {
didSet {
updateUI()
}
}
@IBOutlet weak var futerdimageview: UIImageView!
@IBOutlet weak var interstTitleLabel: UILabel!
private func updateUI()
{
interstTitleLabel.text! = interest2.title
futerdimageview.image = interest2.featuredImage!
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = 10.0
self.clipsToBounds = true
}
}
还有这段代码
import UIKit
class Interest1
{
var title = ""
var description = ""
var featuredImage: UIImage!
var button1: UIButton!
var button2: UIButton!
var button3: UIButton!
var button4: UIButton!
init(title: String, featuredImage: UIImage!)
{
self.title = title
self.featuredImage = featuredImage
}
static func createInterest() -> [Interest1]
{
return [
Interest1(title: "One", featuredImage: UIImage(named:"001.png")!),
Interest1(title: "Two", featuredImage: UIImage(named:"002.png")!),
Interest1(title: "Three", featuredImage: UIImage(named:"003.png")!),
Interest1(title: "Four", featuredImage: UIImage(named:"004.png")!),
]
}
}
【问题讨论】:
-
用 indexPath 检查条件
-
正是@chirag90
-
@chirag90 我试过这段代码,但我想我不知道如何使用它
-
@faten 在线程中显示您需要使用不同的重用标识符注册单元。这需要放入 viewdidload 方法中。在collectionview cellForItemAt 中,您可以开始使用其他标识符
标签: ios swift xcode uicollectionview uicollectionviewcell