Skip to content
Search

IOS:UITableView

lastmod 2018-08-17created 2014-02-0716 edits

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

Count UITableView Row

The way to get the number of rows in a UITableView is shown below.

- (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

The way to find a custom UITableViewCell (with XIB) in a UITableView is shown below.

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

A summary of solutions to problems related to UITableView.

cellForRowAtIndexPath not called on reload

How to fix the problem where cellForRowAtIndexPath of UITableViewDataSource is not called properly even when reload of UITableview is called.

Initialize check

Check whether you initialized the UITableView as shown below. 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

If it is not called on the Main Thread, you can try the following.

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

To check whether you are on the main thread, you can use [NSThread isMainThread].

heightForRowAtIndexPath: return 0?

You can check that the Cell height is not 0.

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

If it returns 0, cellForRowAtIndexPath is not called.

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 ↩