티스토리 뷰

발생일: 2016.03.23

키워드: UITableViewCell, reuseIdentifier, identifier, NSBundle mainBundle, loadNibNamed, registerNib, forCellWithReuseIdentifier

문제:
코드 리뷰를 받아보니, 내가 테이블뷰셀의 Reuse Identifier 설정을 빼먹었었다.
deque 할 땐 정의했었는데 xib 에서 작성하는 걸 누락했는데, 종종 발생하는 실수라고 한다.


해결책:

xib 로 테이블뷰셀을 만들고, 아래와 같이 NSBundle 의 loadNibNamed 로 xib를 불러오는 방식으로 구현했다면,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sampleCell"];

    if (cell == nil) {
        return [[[NSBundle mainBundle] loadNibNamed:@"SampleCell" owner:nil options:nil] firstObject];
    }

    return cell;
}

인터페이스 빌더의 속성 인스펙터에서 Identifier을 반드시 설정해주도록 한다.


dequeue 할 땐 아이덴티파이어를 넣는데, xib 에서는 종종 빼먹는 실수를 하게 된다고 한다.
디버깅하기도 어려워서, 작업할 때 잘 챙기라는 가이드 ㅎㅎ


새로 작성하는 코드라면, UITableViewCell 의 registerNib:forCellWithReuseIdentifier: 를 사용하는 게 더 낫겠다.
deque 전 적당한 시점(viewDidLoad, awakeFromNib 등)에 아래와 같이 호출해주면 된다.

[tableView registerNib:[UINib nibWithNibName:@"SampleCell" bundle:nil] forCellWithReuseIdentifier:@"sampleCell"];



반응형
댓글
공지사항