insertion and deletion in the table of objective C
I am wondering, I have an empty table and I want to display a text in that table, then I want to clear that text when the table has been inserted with something else. And the text will appear again when I complet开发者_开发百科ely remove everything in the table again.?
In your dataSource for your UITableView there is a method:
-tableView:objectValueForTableColumn:row:
In that method you can check your NSArray or whatever you have supplying data to your table to see if there are any entries. If there are none and the request is for row zero, column zero, just return your 'no data' string.
To clarify syntax, assume you have an NSArray named songs which you wish to print:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//boilerplate cell production
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
//check if we have data to supply, if not set message to "No Songs Available"
//otherwise print the name of the song in each cell
if (indexPath.row == 0 && indexPath.section == 0 && [songs count] == 0)
cell.textLabel.text = @"No Songs Available";
else if (indexPath.row < [songs count])
cell.textLabel.text = [(Song *)[songs objectAtIndex:indexPath.row] songName];
return cell;
}
精彩评论