0

My cells' string text (TDBadgedCell) is only updated when I edit data in the table's child table.

If I edit the same data from a different tab, then go back to this table, the string will not update until I relaunch the app.

I have this code as well:

- (void)viewWillAppear:(BOOL)animated
{
    [self refreshFetchedResultsController];
    [self.logTableView reloadData];
}

Edit:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(TDBadgedCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.textColor = [UIColor blackColor];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [logArray objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    cell.imageView.image = [UIImage imageNamed:@"17-bar-chart.png"];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MMM d, y"]; 
    NSDate *date = nil;

    NSDate *today = [NSDate date]; 
    NSDate *thisWeek  = [today dateByAddingTimeInterval: -604800.0];
    NSDate *thisMonth = [today dateByAddingTimeInterval: -2629743.83];

    if (indexPath.row == 0)
    {
        date = [NSDate date]; 
        NSString *dateString = [dateFormatter stringFromDate:date]; 
        cell.badgeString = dateString;
    }
    else if (indexPath.row == 1)
    {
        if ([[self.fetchedResultsController fetchedObjects]count] > 1)
        {
            self.session = [[self.fetchedResultsController fetchedObjects]objectAtIndex:1];
            NSDate *date = self.session.timeStamp;
            NSString *dateString = [dateFormatter stringFromDate:date]; 
            cell.badgeString = dateString;
        }
        else
        {
            cell.badgeString = @"None";
        }
    }
    else if (indexPath.row == 2)
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"(timeStamp >= %@) AND (timeStamp <= %@)", thisWeek, today]];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];
        NSError *error = nil;
        NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
        NSLog(@"Fetch error: %@", error);

        cell.badgeString = [NSString stringWithFormat:@"%i", [results count]];
        [fetchRequest release];
    }
    else if (indexPath.row == 3)
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(timeStamp >= %@) AND (timeStamp <= %@)", thisMonth, today]];
        [fetchRequest setEntity:[NSEntityDescription entityForName:@"Session" inManagedObjectContext:managedObjectContext]];

        NSError *error = nil;
        NSArray *results = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
        NSLog(@"Fetch error: %@", error);

        cell.badgeString = [NSString stringWithFormat:@"%i", [results count]];
        [fetchRequest release];
    }
    else if (indexPath.row == 4)
    {
        cell.badgeString = [NSString stringWithFormat:@"%i", [[self.fetchedResultsController fetchedObjects] count]];
        NSLog(@"%i", [[self.fetchedResultsController fetchedObjects] count]);
    }

    UIImageView *myImageView = nil; 
    if (indexPath.row == 0 || indexPath.row == 1)
    {
        int myInt = cell.badgeString.length;
        if (myInt > 11)
        {
            myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"silvercell7.png"]];
        }
        else if (myInt < 5)
        {
            myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"silvercell9.png"]];
        }
        else
        {
            myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"silvercell8.png"]];
        }
    }
    else
    {
        myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"silvercell5.png"]];
    }

    [cell setBackgroundView:myImageView]; 
    [myImageView release];
    cell.badgeColor = [UIColor colorWithRed:24/255.0 green:83/255.0 blue:170/255.0 alpha:1.0];
    [cell.badge setNeedsDisplay];
    [dateFormatter release]; 
}

1 Answers1

0

I think, you might be able to see the image for the newly added row. But not text Because you don't have support for that in your code.

In you code, you are always creating the new cell, Try with reusing the cell with below code.

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

    NSMutableString *CellIdentifier  = [[NSMutableString alloc] initWithCapacity:10]
    [CellIdentifier appendFormat:@"Cell_%d_%d",indexPath.section,indexPath.row];

    TDBadgedCell *cell =  (TDBadgedCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell ==  nil)
    {
        TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • Jhaliya, the view I am talking about doesn't have a newly added code. I am talking about the cell.badgeString is not updating when there is a new object in the fetch. –  Jun 04 '11 at 05:05
  • At line `TDBadgedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];`, I am getting the warning saying "incompatible pointer types initializing "TDBadgedCell" with an expression of type UITableViewCell" –  Jun 04 '11 at 05:51
  • Now its crashing saying `*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:` –  Jun 04 '11 at 05:58
  • Thats exactly what I have now, and its still crashing. –  Jun 04 '11 at 06:03