속성

2017-12-05 14 views
0

여기 내 수업속성

class myCell: UICollectionViewCell { 

    public var myProp:String = "" 

    let myControl:UILabel = { 

      let label = UILabel() 
      label.translatesAutoresizingMaskIntoConstraints = false 
      label.Text = myProp 

     return label 
    }() 

} 

내 UI 요소의 생성 내에서 myProp를 사용하고자하지만 컴파일러는 내가 myProp을 사용할 수 없습니다 말하고있다 UICollectionViewCell.

은 또는 왜이 텍스트와 서브 뷰를 추가하는이를 구현해야 cellForRowAtIndex에 렌더링하는 동안

class CollectionViewCell: UICollectionViewCell { 
    public var myProp:String = "" 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

    } 

    func setText() { 
     let myControl:UILabel = { 
      let label = UILabel() 
      label.translatesAutoresizingMaskIntoConstraints = false 
      label.text = myProp 

      return label 
     }() 
     self.addSubview(myControl) 
    } 
} 

을 작동이 잘못된

class myCell: UICollectionViewCell { 

    public var myLabel:UILabel = UILabel() 

    let myControl:UIView = { 
      let ui = UIView() 

      myLabel = { 

       let lbl = UILabel()         

       lbl.translatesAutoresizingMaskIntoConstraints = false 

       return lbl 
      }() 

       ui.AddSubView(myLabel) 

      return ui 
     }() 

    } 

답변

0

입니다.

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell 
    cell.myProp = "text" 
    cell.setText() 

    return cell 
} 
+0

그러나 이것은 myControl을 숨 깁니다. 콜렉션 뷰에서 셀을 렌더링하는 동안 셀을 사용할 때 cell.myProp = "text"와 같은 작업을 수행합니다.이 문제를 해결할 수있었습니다. – Arcadian

+0

셀 초기화 메소드에'UILabel'을 하위 뷰로 추가하려면 값을 설정하지 않습니다. 왜냐하면'cellForRowAtIndex'에서 초기화 될 때 init이 실행되기 때문입니다. 텍스트를 표시하려면 다른 방법이 필요합니다. 나는 곧 나의 대답을 업데이트 할 것이다. –

+0

셀 초기화 중에 UI를 설정했습니다. 모든 ui 요소는 init 중에 만들어지고 제 위치에 있습니다. 내가하는 모든 것은 cellForItemAt 동안 텍스트 값을 설정합니다. – Arcadian