我使用下面的代码来填充tableview行。在单击一行时,我正在重新加载表数据,但行没有更新。在iOS 11之前它运行良好但在更新到iOS 11后我遇到了这个问题:
-(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];
}
我做错了吗?
答案 0 :(得分: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;
}
答案 1 :(得分:0)
在didSelectRowAtIndexPath
方法中,将此代码添加到适用于我的reloadData
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
.
[tableView reloadData];
[self.view setNeedsDisplay];
}