我如何使用 withReuseIdentifier
how can i use withReuseIdentifier
如何使用多个 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")!),
]
}
}
试试这个,在你的 viewDidLoad 方法中添加
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "1")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "2")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "3")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "4")
并更改您的 collectionView cellForItemAt
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
} else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "2", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
} else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "3", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
} else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "4", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
}
return interestCollectionViewCell()
}
如何使用多个 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")!),
]
}
}
试试这个,在你的 viewDidLoad 方法中添加
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "1")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "2")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "3")
self.collectionView.register(interestCollectionViewCell.self, forCellWithReuseIdentifier: "4")
并更改您的 collectionView cellForItemAt
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "1", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
} else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "2", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
} else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "3", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
} else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "4", for: indexPath as IndexPath) as? interestCollectionViewCell
{
cell.interest2 = self.interests3[indexPath.item]
return cell
}
return interestCollectionViewCell()
}