사용자 지정 UICollectionViewCell 클래스를 사용하여 UICollectionView를 설정합니다.문자열이 사용자 지정 클래스에 전달 될 때 UICollectionViewCell의 레이블 텍스트 설정이 작동하지 않습니다.
UICollectionViewDelegate에 지정된 함수를 사용하여 각 셀에 텍스트로 채워진 레이블을 얻을 수있었습니다.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "keypadButton", for: indexPath) as! KeypadButtonCollectionViewCell
cell.awakeFromNib()
return cell
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let button = cell as! KeypadButtonCollectionViewCell
//Assigning like this works
button.buttonLabel.text = tempScale[indexPath.row]
}
그러나,
나는 처음에 (아래로)을 KeypadButtonCollectionViewCell
클래스에서 buttonText
클래스 변수와 다르게 설정하고
class KeypadButtonCollectionViewCell: UICollectionViewCell {
var buttonLabel: UILabel!
var buttonText: String!
override func awakeFromNib() {
buttonLabel = UILabel.init(frame: contentView.frame)
buttonLabel.font = UIFont.init(name: "HelveticaNeue-Ultralight", size: 20)
buttonLabel.textColor = UIColor.black
buttonLabel.textAlignment = NSTextAlignment.center
buttonLabel.text = buttonText //Assigning here didn't work
contentView.layer.borderColor = UIColor.init(red: 37/255, green: 37/255, blue: 37/255, alpha: 1).cgColor
contentView.layer.borderWidth = 4
contentView.addSubview(buttonLabel)
}
}
//---------In the view controller--------
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let button = cell as! KeypadButtonCollectionViewCell
//Setting class var string here to be set as label.text not working
button.labelText = tempScale[indexPath.row]
}
그것이 내가 여기에 무엇을 오해하고있다 (또한 아래) willDisplay
함수에서 그 변수를 설정했다 ? wakeFromNib() 메서드에서 레이블 텍스트를 설정하기 위해 할당 된 클래스 변수를 사용하도록 설정하는 것이 좋지 않지만 레이블 텍스트를 직접 설정할 때 효과가있는 이유는 무엇입니까?
처음에 언급했듯이, 나는 이것을하기위한 작업 방법이있다. 나는 이것을 학계에 관심이 있고 OOP 프로그래밍을 더 잘 이해하고있다.