Suppose I have a UITableView with two sections. If the data source for that section is empty I would like to display a placeholder cell with the text “Section Name is empty.”
How can I do that?
Code
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if(section == 1)
{
return @"Section One";
}
if(section == 0)
{
return @"Section Two";
}
return @"";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1)
{
return self.sectionOne.count;
}
else
{
return self.sectionTwo.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSArray *theSource =[[NSArray alloc] init];
if(indexPath.section == 1)
{
theSource = self.sectionOne;
}
else
{
theSource = self.sectionTwo;
}
// See if there's an existing cell we can reuse
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil)
{
// No cell to reuse => create a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
cell.backgroundView = [[UIImageView alloc] init];
// Continue creating cell
}
}
Implement the following functions in your UITableViewDataSource (pseudo code):
And: