0
私はカスタムのUITableViewCell
コレクションビューを持っています。このクラスには、表示されるコレクションビューセルの数を含む曖昧な「nbElements」があります。 dequeueReusableCell
でtableviewセルを作成するときに、nbElementsの値をどのように初期化できるかを知りたいですか?ここでswift:tableViewcellの初期化
は、カスタムテーブルビューセルのコードです:ここで
class CustomTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
var nbElements = Int!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.collectionView.register(UINib(nibName: "CustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "customCollectionViewCell")
self.collectionViewHeightConstraint.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.height
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return nbElements
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCollectionViewCell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellWidth = (self.collectionView.frame.width/2.0) - 1
let size = CGSize.init(width: cellWidth, height: cellWidth)
return size;
}
}
は、私はテーブルビューのセルを作成するコードは次のとおりです。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customTableViewCell", for: indexPath) as! CustomTableViewCell
cell.nbElements = 4
return cell
}
よろしく。
コードに問題がありますか?しかし、 'nbElements'を非オプションの' var nbElements = 0'と宣言すると、あなたの行はコンパイルされません。 – vadian
@vadianこの例では、collectionViewに4つのセルを表示したかったのですが、そうではありません。 – Hero
コレクションビューではどこで 'reloadData()'を呼び出しますか? – vadian