scientiffic
scientiffic

Reputation: 9415

Cannot refresh cell textLabel in tableView

In my iOS app, I want to fresh the entire contents of a tableView when the viewController is loaded. Each cell of a tableView has a textLabel that is the name of a step object.

I've ensured that when I return to the viewController, the correct stepNames are loaded (I know this by logging the names of the steps). However, the name of the step is not updated in the tableView.

How can I ensure that the labels of the tableView cells are loading properly?

How I try to trigger a refresh of the TableView:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    if(reloadSteps == YES){
        NSLog(@"reloading steps");
        NSData * stepsData = [NSData dataWithContentsOfURL:stepsURL];
        [self fetchProjectSteps:stepsData];
        [self.projectInfo reloadData];
        int numRows = [stepNames count];
        NSLog(@"numRows %i", numRows);
        for(int i =0; i<numRows; i++){
            [self tableView:self.projectInfo cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        }
    }
}

How each cell of the tableView is rendered:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"in cellForRowAtIndexPath");
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StepCell"];
    }
    if (indexPath.row ==0) {
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
        cell.textLabel.text = @"Project Description";
        cell.textAlignment = NSTextAlignmentCenter;
    } else {
        cell.userInteractionEnabled = YES;
        cell.textLabel.font = [UIFont systemFontOfSize:16.0];
        cell.textLabel.text = [stepNames objectAtIndex:indexPath.row];
        NSLog(@"textLabel: %@", cell.textLabel.text); // THIS IS CORRECT, BUT DOES NOT APPEAR TO BE UPDATED IN THE TABLEVIEW

        if(![[stepImages objectAtIndex:indexPath.row] isEqual: @""]) {
            cell.accessoryView = [stepImages objectAtIndex:indexPath.row];
        }
    }
    return cell;
}

Here is my header file:

@interface EditProjectViewController : UIViewController <UIActionSheetDelegate, UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>{
  @property (strong,nonatomic) IBOutlet UITableView *projectInfo;
}

and then in my implementation file:

-(void) viewDidLoad{
    self.projectInfo = [[UITableView alloc]init];
    self.projectInfo.delegate = self;
}

Upvotes: 0

Views: 220

Answers (1)

rebello95
rebello95

Reputation: 8576

Delete all this:

    int numRows = [stepNames count];
    NSLog(@"numRows %i", numRows);
    for(int i =0; i<numRows; i++){
        [self tableView:self.projectInfo cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    }

Your reloadData call will call cellForRowAtIndexPath: for every cell in your table view. Just make sure you're returning stepNames.count for numberOfRowsInSection:.

EDIT

I've looked over your updated code. A couple things (including what your problem most likely is):

  1. Your projectInfo outlet should be weak, not strong (since it's an IBOutlet)
  2. Even though you have your table view linked up in the interface file, you're initializing it in viewDidLoad, creating a new instance

Change the following:

-(void) viewDidLoad{
    self.projectInfo = [[UITableView alloc]init];
    self.projectInfo.delegate = self;
}

To this:

-(void) viewDidLoad{
    self.projectInfo.dataSource = self;
    self.projectInfo.delegate = self;
}

Upvotes: 1

Related Questions