views:

26

answers:

1

Hi,

Using the following code I have been able to display a text message when there is no data to display in a uitableView:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   {
// Return the number of rows in the section.
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

if ([appDelegate.myDataArray count] == 0)
{
    return 3;
}

return [appDelegate.myDataArray count];
}


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


static NSString* myCellIdentifier = @"MyCell";

   myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];

if ([appDelegate.myDataArray count] == 0)
{   
    if (indexPath.row == 2)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here

        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = (@"No Records Found");
        return cell;
    }
    else 
    {
        // return a blank row for spacing
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellIdentifier]; // only reuse this type of cell here

        if (cell == nil) 
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCellIdentifier] autorelease];
        }
        cell.textLabel.text = (@"");
        return cell;
    }
} ........

My problem is that I want to have this behaviour when I have deleted the records of a table using the method:

- (void)tableView:(UITableView *)tv  commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath {

Currently, the result is a crash when I delete the last record: "...The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (1)..."

Any ideas of how I can have the text displayed 3 rows down (as shown above) and not crash after I remove the last record while deleting?

Thanks.

A: 

While I wouldn't use the same approach to achieve the "no results" feedback, I think your error indicates a problem in the transition when the last "real" row is deleted.

The way to fix it is to tell the table that you inserted 3 rows when the last one is deleted. That or force reload the table view [self.tableView reloadData] at that point to ensure it doesn't get confused with the transition.

ohhorob
Hi, thanks for the response. I'm all for another approach to achieve the "no results". If you have a suggestion, I'd try it out on my end. It might make everything easier.
Vivas
I would add an overlay `UIView` that contains the "No records found" label. You can design the view in IB and hook it up to a retained `IBOutlet` property, or create it as needed. If you are using a subclass of `UITableViewController`, you will need to exchange the tableView for the overlay view. If not, you can just add the overlay view to `self.view`.
ohhorob
Thanks. It makes sense. I was using an overlay when an UIActivityIndicator is required but yeah, it makes sense to use it in this case as well.
Vivas