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

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }



        [cell setBackgroundColor:[UIColor lightGrayColor]];
        cell.textLabel.textColor = [UIColor lightTextColor];
        cell.textLabel.backgroundColor = nil;
        cell.detailTextLabel.textColor = [UIColor darkGrayColor];
        cell.detailTextLabel.backgroundColor = nil; 

  // Get list of local notifications
        NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
        UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];

        // Display notification info
        [cell.textLabel setText:notif.alertBody];

        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        [format setDateFormat:@"MMM dd 'at' HH:mm"];

        NSString *dateString = [format stringFromDate:notif.fireDate];

        NSString *textData = [[NSString alloc]initWithFormat:@"%@",dateString];

        [cell.detailTextLabel setText:textData];

        //[notif.fireDate description]



        [self.tableView reloadData];
        return cell;

    }

Everything goes absoloutely normal if i DONT write "[self.tableView reloadData];", but if I do, seems like the cell is there but its not being displayed (the cell separator disappear according to the number of cells the tableview has)

I need to reloadData so the user won't need to get out of the view and load the view again to display updated information.

Any suggestion?

  • As Omar said, this is the wrong place to reload the data. Make a `newItem` method and do a `[tableView reloadData]` there instead. Or when you remove / clear items. –  Jun 24 '12 at 11:09
  • Would you please give me an example? – Lucas Vallim da Costa Jun 24 '12 at 11:11
  • `reloadData` is only used to tell the Table View that the data should be reloaded from your controller (ie some delegate methods such as the one you have posted above will be called). Have a look at [this](http://www.iphonesdkarticles.com/2009/01/uitableview-creating-simple-table-view.html) if you haven't already. –  Jun 24 '12 at 11:30

2 Answers2

3

You dont need to add [self.tableView reloadData]; inside cellForRowAtIndexPath, this is absolutely wrong, since calling reloadData will again call cellForRowAtIndexPath which as you experienced will create very weird issues

In general you normally call [self.tableView reloadData]; whenever you want to change all or some of the contents of the UITableView

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
1

Create a function to check if new data is available (maybe implement a refresh button/pull to refresh). If new data is available call [self.tableView reloadData];.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Joshua Lückers
  • 112
  • 1
  • 9