2017-09-26 100 views
1

我使用下面的代碼來填充tableview行。在單擊一行時,我正在重新加載表數據,但行未被更新。 iOS的11之前,它工作正常,但在更新到iOS 11後我面對這個問題:UITableview單元格在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]; 

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; 
} 

,我在didSelectRowAtIndexPath方法是這樣重裝數據:

-(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]; 
} 

難道我做錯了什麼?

+1

如何將宣佈'_selectedDays' –

+0

我初始化'_selectedDays'在'viewDidLoad'爲'_selectedDays = [NSMutableArray的新]' –

+0

你把對象追加到'_selectedDays' –

回答

2

我得到了蘋果開發者論壇的答案。我不得不在接口中聲明幾天,並在viewDidLoad中初始化它,並在didSelectRowAtIndexPath中更改代碼。所以,現在我的代碼看起來是這樣的:

@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; 
} 

參考:https://forums.developer.apple.com/message/263637#263637

0

didSelectRowAtIndexPath方法添加此代碼reloadData它爲我工作

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

我嘗試過,但沒有爲我工作。我已經添加了我的答案,對我有用。 –

相關問題