2017-05-23 1 views
1

많은 사용자 지정 셀 (xib 파일 + UITableViewCell의 신속한 하위 클래스)이 있고 많은 장면에서이를 동적 셀로 사용하고 모든 것이 잘 작동합니다 (셀 등록, 큐 제거 등).프로그래밍 방식으로 사용자 정의 정적 셀

해당 셀을 사용할 수있는 정적 tableView가 필요할 때 제 문제가 발생합니다. 스토리 보드를 사용하여 재사용 가능성을 잃어 버리기 때문에이 작업을 수행하고 싶지 않습니다.

나는 아래와 같이 시도했지만 보통 UITableViewCell으로, 내 설계된 xib CustomCell 대신에 표시됩니다. 펜촉의 셀 하위 뷰는로드되지 않는 것 같습니다.

private lazy var cells: [UITableViewCell] = [ 
    CustomCell1(), 
    CustomCell2(), 
    CustomCell3(), 
] 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return cells.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    return cells[indexPath.row] 
} 

프로그래밍 방식으로 정적 사용자 지정 셀을 사용하여 UITableView을 만드는 방법은 무엇입니까?

수정 @thedp에 대한 답변입니다. 맞춤형 셀은 이와 같이 설계되었습니다. 이것은 단지 예입니다 :

final class CustomCell: UITableViewCell { 
    @IBOutlet weak private var titleLabel: UILabel! 
    @IBOutlet weak private var textView: MyCustomTextView! 
    @IBOutlet weak private var doneButton: MyCustomButton! 
    func configureCell(with: ...) { ... } 
} 
+0

'CustomCell'에 대한 코드를 공유 할 수 있습니까? – thedp

+1

@thedp 주제 편집 : 관심을 보여 주셔서 고마워요. –

답변

0

1) 동적 인 UITableView를 위해 일반적으로하고있는 것처럼 셀을 등록하십시오. jQuery과

의 데이터 소스를 설정

2) 개인 게으른 VAR 세포에서

3) 저장 셀 식별자 :있는 UITableViewCell] 대신 다음

override func tableView(_ tableView: UITableView, 
    cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cellIdentifer = cells[indexPath.row] 
    let cell = tableView.dequeueReusableCell(withIdentifier: 
    cellIdentifer, for: indexPath) 
    return cell 
} 

또한 당신이 다음에 무엇을 할당 세포

4) viewDidLoad() 메서드에서 셀을 dequeue하여 배열에 저장할 수 있습니다. 하지만 dequeueReusableCell이 필요합니다. 클래스에서 할당 할 수 있습니다.

관련 문제