简体   繁体   中英

UITableview cell not updating on reloaddata

I am using below code to populate tableview rows. On clicking a row I am reloading table data but row is not being updated. Before iOS 11 it was working fine but after updating to iOS 11 I am facing this issue:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DayCell" forIndexPath:indexPath];

UILabel *label = [cell viewWithTag:101];
UIImageView *imageView = [cell viewWithTag:102];

NSArray *days = @[@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"];
label.text = [days objectAtIndex:indexPath.row];

if ([_selectedDays containsObject:label.text]) {
    imageView.image = [UIImage imageNamed:@"CheckedIcon"];
} else {
    imageView.image = nil;
}

return cell;
}

And I am reloading data in didSelectRowAtIndexPath like this:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];

UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
UILabel *label = [cell viewWithTag:101];

if ([_selectedDays containsObject:label.text]) {
    [_selectedDays removeObject:label.text];
} else {
    [_selectedDays addObject:label.text];
}

[tableView reloadData];
}

Am I doing something wrong?

I got the answer on apple developer forum. I had to declare array of days in interface and initialize it in viewDidLoad and change code in didSelectRowAtIndexPath . So now my code looks like this:

@interface SomeTableViewController {  

@property (nonatomic, strong) NSArray *days;  
@property (nonatomic, strong) NSMutableArray *selectedDays;  

}  
...  
- (void)viewDidLoad {  
  [super viewDidLoad];  

  _days = @[@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"];  
  _selectedDays = @[];  

   [tableView reloadData];  
  ...  
}  

...  

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DayCell" forIndexPath:indexPath];  

    UILabel *label = [cell viewWithTag:101];  
    UIImageView *imageView = [cell viewWithTag:102];  

    NSString *text = _days[indexPath.row];  

    label.text = text;  

    if ([_selectedDays containsObject:text]) {  
        imageView.image = [UIImage imageNamed:@"CheckedIcon"];  
    } else {  
        imageView.image = nil;  
    }  

    return cell;  
}  

...  

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  
    [tableView deselectRowAtIndexPath:indexPath animated:NO];  

     NSString *text = _days[indexPath.row];  

    if ([_selectedDays containsObject:text]) {  
        [_selectedDays removeObject:text];  
    } else {  
        [_selectedDays addObject:text];  
    }  

    [tableView reloadData];  
}  

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  return _days.count;  
}  

Reference: https://forums.developer.apple.com/message/263637#263637

In didSelectRowAtIndexPath method add this code with reloadData it works for me

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
.
.
.
.
[tableView reloadData];
[self.view setNeedsDisplay];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM