UITableview 单元格未在重新加载数据时更新
UITableview cell not updating on reloaddata
我正在使用以下代码来填充 table 查看行。单击一行时,我正在重新加载 table 数据,但行未更新。在 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];
}
我是不是做错了什么?
在 didSelectRowAtIndexPath
方法中添加此代码和 reloadData 它对我有用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
.
[tableView reloadData];
[self.view setNeedsDisplay];
}
我在苹果开发者论坛上得到了答案。我必须在接口中声明天数数组并在 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;
}
我正在使用以下代码来填充 table 查看行。单击一行时,我正在重新加载 table 数据,但行未更新。在 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];
}
我是不是做错了什么?
在 didSelectRowAtIndexPath
方法中添加此代码和 reloadData 它对我有用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
.
[tableView reloadData];
[self.view setNeedsDisplay];
}
我在苹果开发者论坛上得到了答案。我必须在接口中声明天数数组并在 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;
}