Jack Nutkins
Jack Nutkins

Reputation: 1555

Changing Properties of a static cell in a UITableView when view appears

Im using the following code to attempt to update several static cells in my UITableView:

- (void) viewDidAppear:(BOOL)animated{
    [self reloadTableData];
}

- (void) reloadTableData {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:8];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
    if ([[userDefaults objectForKey:@"canDeleteReceipts"] isEqualToString:@"0"])
    {
        cell.contentView.alpha = 0.2;
        cell.userInteractionEnabled = NO;
    }
    else 
    {
        cell.contentView.alpha = 1;
        cell.userInteractionEnabled = YES;
    }

    path = [NSIndexPath indexPathForRow:1 inSection:8];
    cell = [self.tableView cellForRowAtIndexPath:path];
    if ([[userDefaults objectForKey:@"canDeleteMileages"] isEqualToString:@"0"])
    {
        cell.contentView.alpha = 0.2;
        cell.userInteractionEnabled = NO;
    }
    else 
    {
        cell.contentView.alpha = 1;
        cell.userInteractionEnabled = YES;
    }

    path = [NSIndexPath indexPathForRow:2 inSection:8];
    cell = [self.tableView cellForRowAtIndexPath:path];
    if ([[userDefaults objectForKey:@"canDeleteAll"] isEqualToString:@"0"])
    {
        cell.contentView.alpha = 0.2;
        cell.userInteractionEnabled = NO;
        NSLog(@"%@", cell);
    }
    else 
    {
        cell.contentView.alpha = 1;
        cell.userInteractionEnabled = YES;
    }

}

However it does't work, the code is called but does nothing. Can anyone explain how I update the attributes of a static cell to achieve something like the above when the view appears?

The NSLog statement says that the cell is null.

Thanks

Jack

Upvotes: 0

Views: 998

Answers (4)

Ishank
Ishank

Reputation: 2926

This is not the proper way to reload the UITableView.

U have to give the message [self.tableView reload] in the - (void) viewDidAppear:(BOOL)animated --> go to the DataSource of your table view i.e -(UITableViewCell*)cellForRowatIndexPath:(NSIndexPath indexPath), there have your respective cell using the method- [table cellForRowAtIndexPath] and do what u r trying in the - (void) reloadTableData method of yours.

Upvotes: 0

Ratnakar
Ratnakar

Reputation: 443

Here is the link.Go through this tutorial and make some changes according to your application.I think it works for you.

Upvotes: 0

san
san

Reputation: 3328

You need to write the code in - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath instead of your reloadData method.

Upvotes: 2

Apurv
Apurv

Reputation: 17186

In that case you should cache your cells in an NSArray. When required, use the cell from that array and do not fetch from tableView.

Upvotes: 0

Related Questions