gpt4 book ai didi

iOS 7 - UITableView 不保持突出显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:37 24 4
gpt4 key购买 nike

抱歉,如果这是一个简单的问题,但我的代码似乎无法正常工作。我想在用户选择后突出显示 tableView 中的选定行。

我有 2 个数组,我从中获取价格和商品名称。我有第三个数组,用于跟踪选择了哪一行,因此我知道已经选择了哪些行。

这是我的 cellForRowAtIndexPath 方法,我在其中分配了文本标签和复选标记。

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

static NSString *ItemCellIdentifier = @"CellIdentifier";

//SearchresultTableViewCell is my own class for the tableViewCell
SearchResultTableViewCell *tableCell =(SearchResultTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier];

//set text labels here
[tableCell.price setText:[NSString stringWithString:[priceList objectAtIndex:indexPath.row]]];
[tableCell.itemName setText:[NSString stringWithString:[itemNameList objectAtIndex:indexPath.row]]];

//set check mark and highlights.
if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
tableCell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableCell setHighlighted:YES];
}

else{
tableCell.accessoryType = UITableViewCellAccessoryNone;
[tableCell setHighlighted:NO];
}

return tableCell;

}

这是他的 didSelectRowAtIndexPath 方法。我在这里基本上做的是通过编辑我的 checkList 数组将对象标记为已检查。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"did select row at index path entered");
if ([[checkList objectAtIndex:indexPath.row] integerValue] == 1){
[checkList replaceObjectAtIndex:indexPath.row withObject:@"0"];

}
else{
[checkList replaceObjectAtIndex:indexPath.row withObject:@"1"];
}
[self.searchResults reloadData];
}

我已经尝试了 [tableCell setHighlighted:TRUE];[tableCell setHighlighted:1]; 但它不起作用。

有什么想法吗?谢谢。

最佳答案

感谢您回复我的评论。正如我提到的,发送 reloadData 消息将导致取消选择当前选定的单元格。因此,您应该在重新加载 tableView 之前缓存当前选定的单元格,然后再恢复它们。我在想这样的事情:

NSArray *indexPaths = [self.searchResults indexPathsForSelectedRows];

[self.searchResults reloadData];

for (NSIndexPath *indexPath in indexPaths) {
[self.searchResults selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

只要您不插入/删除/移动行,这就应该有效。如果是,则必须对此负责。

关于iOS 7 - UITableView 不保持突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265838/

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