gpt4 book ai didi

ios - 所选 UITableViewCell 的突出显示不一致

转载 作者:可可西里 更新时间:2023-11-01 04:52:16 25 4
gpt4 key购买 nike

我有一个 UITableView,如果数据模型表明该单元格代表项目列表中的选定选项,我需要在其中以编程方式选择一个单元格。我在配置 UITableViewCell 时这样做:

if (group == self.theCase.assignedGroup) {
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
self.selectedIndexPath = indexPath;
} else {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

我看到了非常奇怪的行为。如果 tableview 的第一行是应该选择的行,则单元格不会正确突出显示其背景。但是,如果第二行是应该选择的那一行,它会按预期工作(最后的屏幕截图)。

更新:这可能与我异步加载表格数据有关,并且在加载数据时我显示了另一种带有进度指示器的单元格第一排。这是处理此问题的 TableView 数据源代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if (self.hasMorePages) {
return self.groups.count + 1;
}
return self.groups.count;
}

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

if (indexPath.row < self.groups.count) {
cell = [tableView dequeueReusableCellWithIdentifier:@"DSAssignCell" forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"DSLoadingCell" forIndexPath:indexPath];
[self configureLoadingCell:cell forRowAtIndexPath:indexPath];
}

return cell;
}

- (void)configureCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
DSGroup *group = self.groups[indexPath.row];
if (group == self.theCase.assignedGroup) {
[self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
self.selectedIndexPath = indexPath;
} else {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
cell.textLabel.text = group.name;
cell.tag = kDataCellTag;
}

- (void)configureLoadingCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIActivityIndicatorView *activityIndicator;
if ([cell viewWithTag:kActivityIndicatorTag]) {
activityIndicator = (UIActivityIndicatorView *)[cell viewWithTag:kActivityIndicatorTag];
} else {
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = cell.center;
activityIndicator.tag = kActivityIndicatorTag;
[cell.contentView addSubview:activityIndicator];
}
[activityIndicator startAnimating];
cell.tag = kLoadingCellTag;
}

更新 根据要求,这里是处理从 Web 服务异步加载组和代理数据的代码:

- (void)viewDidLoad
{
[super viewDidLoad];

[self resetData];
[self loadData];
}

- (void)resetData
{
self.currentPage = 0;
self.hasMorePages = YES;
self.groups = [[NSMutableArray alloc] initWithCapacity:kGroupsPerPage];
self.agents = [[NSMutableArray alloc] initWithCapacity:kAgentsPerPage];
}

- (void)loadData
{
if (self.showingGroups) {
[DSGroup fetchGroupsOnPage:self.currentPage + 1 perPage:kGroupsPerPage success:^(NSArray *groups, NSDictionary *links, NSNumber *totalEntries) {
[self.groups addObjectsFromArray:groups];
[self didLoadDataPage:(links[@"next"] != [NSNull null])];
} failure:^(NSError *error) {
[self showAlert:@"Could not load groups. Please try again later." withError:error];
}];
} else {
[DSUser fetchUsersOnPage:self.currentPage + 1 perPage:kAgentsPerPage success:^(NSArray *users, NSDictionary *links, NSNumber *totalEntries) {
[self.agents addObjectsFromArray:users];
[self didLoadDataPage:(links[@"next"] != [NSNull null])];
} failure:^(NSError *error) {
[self showAlert:@"Could not load users. Please try again later." withError:error];
}];
}
}

- (void)didLoadDataPage:(BOOL)hasMorePages
{
self.hasMorePages = hasMorePages;
self.currentPage++;
[self.tableView reloadData];
}

这是尝试选择(并突出显示)第一行的屏幕截图,这是错误的(没有灰色背景): Trying to select row 0

这是尝试选择(并突出显示)第二行的屏幕截图,这是正确的: [Trying to select row 1

知道这里会发生什么吗?

最佳答案

我无法使用 UITableViewCell 的内置选择样式来解决这个问题,但是对其进行子类化并覆盖 setSelected:animated 修复了它:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

self.contentView.backgroundColor = selected ? [UIColor grayColor] : [UIColor whiteColor];
}

关于ios - 所选 UITableViewCell 的突出显示不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20548212/

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