user1048042
user1048042

Reputation: 115

Howto? In TableView, if NSMutableArray is empty, show other Array

In a tableview I'm displaying local soccer matches from an NSMutableArray that are planned for this week. If there are no matches, I want to display a cell saying something like: 'No matches this week'. I think I have to call for another array, or probably dictionary, if the NSMutableArray of matches is empty, but at this point I have no idea where to start. Any thoughts on how to achieve this?

Upvotes: 1

Views: 1021

Answers (3)

jrturton
jrturton

Reputation: 119242

Both of the answers given are fine, but depending on what you do with the table you may hit some problems later on - for example, if you insert or delete the first row in the table (i.e. adding the first match, or deleting the last match) then you will have an exception raised because the number of rows in the section hasn't changed, but you have added or deleted a row.

You also may need to prevent deletion etc of the special row. It can all get a bit messy.

If this is an issue you may find it better to have the "No matches" message displayed in a header or footer view, which you toggle the visibility of appropriately.

Upvotes: 1

Jeremy
Jeremy

Reputation: 9030

in your table's tableView:numberOfRowsInSection: delegate method, you'll need to know if there are any matches. If not, return 1. This will guarantee you one row in your table.

Then, in your tableView:cellForRowAtIndexPath: delegate method, if there are no matches, you return a cell with the text "No Matches", otherwise, you return a cell according to the match:

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {

   BOOL hasMatches = [myMatchesArray count] > 0;
   return hasMatches ? [myMatchesArray count] : 1;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  BOOL hasMatches = [myMatchesArray count] > 0;

  UITableViewCell *cell = .....

  if (hasMatches) {

       MyMatchObject *match = (MyMatchObject  *)[myMatchesArray objectAtIndex:indexPath.row];

        [cell.textLabel setText:match.matchText];

  }else{
        [cell.textLabel setText:@"No Matches"];
  }

  return cell;

}

Upvotes: 0

Alex Coplan
Alex Coplan

Reputation: 13371

First, test if there are matches. If there are, tell the tableView there are as many rows as matches. If there aren't, return 1 row.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [matches count] ? [matches count] : 1;
}

Then, when creating your cell, check if there are matches, if there are, show the appropriate one, if there aren't, show "No Matches".

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // boiler plate cellForRowAtIndexPath stuff here...

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [matches count] ? [matches objectAtIndex:row] : @"No Matches";
    return cell; 
}

Upvotes: 2

Related Questions