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
Use Main Thread
If it is not called on the Main Thread, you can try the following.
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
- Developer apple: UITableView Class Reference
- IOS UITableView Custom Cell
- Setting the Cell height of a UITableView
- On iPhone 3.0, use rowHeight to set the height of a UITableViewCell
- Changing header and footer height in a UITableView
- Adjusting cell height by the number of text lines in a UITableViewCell
UITableView Tutorial
- Mastering UITableView (1) Delegate implementation theory 1
- Mastering UITableView (2) Groups and cells 2
- Mastering UITableView (3) Cell structure 3
- Mastering UITableView (4) Table editing 4
- Mastering UITableView (5) Index 5
- Mastering UITableView (6) Custom cells 6