如果调出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}}如何保持突出显示?
答案 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: