即使在reloadData之后,UITableViewCell如何保持突出显示?

时间:2014-10-24 08:48:52

标签: ios uitableview reloaddata

如果调出reloadData,则所选的UITableViewCell不会突出显示。所以我必须记住所选单元格的NSIndexPath(没有多重选择)。但是,如果我想在setHighlighted中使用cellForRowAtIndexPath,则不会突出显示单元格。如果我在UITableViewController的方法中执行此操作,则单元格会突出显示!?

除了这个问题,我经历了另一件事。如果我将setHighlighted与" normal"一起使用,我会得到一种多重选择。选择,以便同时突出显示两个单元格(他们不应该这样做)。我尝试用deselectRowAtIndexPath撤消突出显示,但这没有做任何事情。它只能在didSelectRowAtIndexPath中使用当前索引路径,或者在UITableViewController方法中仅在reloadData之后与- (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"testCell"]; UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)]; self.navigationItem.rightBarButtonItem = anotherButton; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { [self.tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) { [self.tableView setLayoutMargins:UIEdgeInsetsZero]; } } -(void)refreshPropertyList:(UIBarButtonItem *)sender { [self.tableView reloadData]; NSIndexPath *temp = [NSIndexPath indexPathForRow:0 inSection:0]; UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:temp]; [cell setHighlighted:true]; self.selectedIndexPath = temp; } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"testCell" forIndexPath:indexPath]; if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } cell.textLabel.text = @"Test"; if ([indexPath isEqual:self.selectedIndexPath]) { [cell setHighlighted:YES]; } else { [cell setHighlighted:NO]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:self.selectedIndexPath animated:false]; self.selectedIndexPath = indexPath; } 一起使用。

我在iOS 7和iOS 8上遇到过这种情况。下面是一个示例代码:

UITableViewCell

即使在reloadData之后{{1}}如何保持突出显示?

1 个答案:

答案 0 :(得分:1)

您可以使用一行代码选择一行

[self.tableView selectRowAtIndexPath:self.selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

选择和突出显示是不同的。因此,您无法取消选择突出显示的状态。访问:https://developer.apple.com/library/ios/documentation/uikit/Reference/UITableViewCell_Class/index.html#//apple_ref/occ/instm/UITableViewCell/setSelected:animated