Skip to content

IOS:UITableView

An instance of UITableView (or simply, a table view) is a means for displaying and editing hierarchical lists of information.

Count UITableView Row

UITableView의 Row개수를 획득할 수 있는 방법은 아래와 같다.

- (int) countTableViewRow
{
    int sections = [_tableView numberOfSections];
    int rows = 0;

    for (NSInteger sectionCount = 0; sectionCount < sections; sectionCount++) {
        rows += [_tableView numberOfRowsInSection:sectionCount];
    }

    return rows;
}

How to add custom cell

UITableView에 사용자 정의 UITableViewCell(with XIB)을 찾아내는 방법은 아래와 같다.

CustomTableCell *cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTableCell" owner:self options:nil];

    for (id currentObject in topLevelObjects){
        if ([currentObject isKindOfClass:[UITableViewCell class]]){
            cell =  (CustomTableCell*) currentObject;
            break;
        }
    }
}

Troubleshooting

UITableView와 관련된 문제점 해결방법 정리.

cellForRowAtIndexPath not called on reload

UITableviewreload를 호출해도 UITableViewDataSourcecellForRowAtIndexPath가 정상적으로 호출되지 않을 경우의 문제점을 해결하는 방법이다.

Initialize check

아래와 같은 방식으로 UITableView를 초기화 했는지 확인해 보자. You have no xib then where you are declared/sets your tableview's properties. Like

self.tableView.frame = CGRectMake(0, 45, 320, 500);
self.tableView.rowHeight = 34.0f;
self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];

[self.tableView setBackgroundColor:[UIColor clearColor]];
self.tableView.showsVerticalScrollIndicator=NO;
self.tableView.dataSource = self;
self.tableView.delegate = self;

Try with

 @property(nonatomic,strong) NSMutableArray *activityKeys;

Use Main Thread

만약 Main Thread에서 호출하지 않을 경우 아래와 같이 시도해 볼 수 있다.

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

메인스레드인지 확인하기 위하여 [NSThread isMainThread]를 사용할 수 있다.

heightForRowAtIndexPath: return 0?

Cell의 높이가 0이 아닌지 확인해 볼 수 있다.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ...
    return 0.0f; // error!
}

만약 0을 반환할 경우 cellForRowAtIndexPath가 호출되지 않는다.

See also

Favorite site

UITableView Tutorial

References


  1. Lecture_of_UITableView_01.pdf 

  2. Lecture_of_UITableView_02.pdf 

  3. Lecture_of_UITableView_03.pdf 

  4. Lecture_of_UITableView_04.pdf 

  5. Lecture_of_UITableView_05.pdf 

  6. Lecture_of_UITableView_06.pdf