I have two sections in a TableView, having their respective sectionHeaders. numberOfRowsInSection is counted dynamically & it might also come out to be 0. So i want to display a default text somewhere in the section in case of 0 rows. How do i do this ? (Working on iOS 6, XCode-4.2)
Why don't you display the default text in a cell in the "empty section"? Instead of returning 0 rows return 1 and place the default text in there. It can be something like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Verify is the section should be empty or not
if(emptySection == NO) {
return numberOfRowsInSection;
}
else {
return 1;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if(emptySection && indexPath.row == 0) {
cell.textLabel.text = @"This is the default text";
}
else {
// Display the normal data
}
return cell;
}
Update
The following code will avoid any actions when tapping an a cell that contains the default text.
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(emptySection) {
return;
}
// Perform desired action here
}
Another solution is to completely prevent the cell from being selected:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)path
{
if(emptySection) {
retur nil;
}
return path;
}