gpt4 book ai didi

ios - UITableview 单元格未在 reloaddata 上更新

转载 作者:行者123 更新时间:2023-12-01 19:50:05 25 4
gpt4 key购买 nike

我正在使用下面的代码来填充 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];
}

难道我做错了什么?

最佳答案

我在苹果开发者论坛上得到了答案。我必须在界面中声明天数数组并在 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

关于ios - UITableview 单元格未在 reloaddata 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46418769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com