gpt4 book ai didi

iphone - UITableView 在 cellForRow 中使用 selectRowAtIndexPath 导致双高亮

转载 作者:行者123 更新时间:2023-11-29 11:06:44 24 4
gpt4 key购买 nike

背景:我正在尝试实现一个可以折叠和展开的分段表格 View 。折叠时,表格会记住选择的内容并在展开时保留选择状态。

问题:a 部分已扩展并正常运行。该单元格已按应有的方式被选中。但是,位于同一位置但下一节的单元格似乎也突出显示了。

示例:我有 2 个部分:AB。在每个部分中,我有 3 个单元格:abc。我展开这两个部分,以便所有单元格都显示在屏幕上并且没有选择任何内容。然后我点击 (A, a)。单元格 (A, a) 突出显示,一切正常。我折叠了 A 部分,然后展开了 A 部分。 (A, a) 应该高亮显示。但是,(B, a) 也被突出显示。这可以用 (A, b) 和 (A, c) 重复。它会分别点亮 (B, b) 和 (B, c)。

项目:<已移除

代码:

viewController.m 对此有详细说明。

@interface ViewController ()

@end

@implementation ViewController{
UITableView * _tableView;

BOOL aOpen;
BOOL bOpen;
BOOL cOpen;

NSIndexPath *selectedIndexPath;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.allowsMultipleSelection = NO;
[self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Table View stuff
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section A" section:section isOpen:aOpen delegate:self];
break;
case 1:
return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section B" section:section isOpen:bOpen delegate:self];
break;
case 2:
return [[TableViewHeader alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40) title:@"Section C" section:section isOpen:cOpen delegate:self];
break;
default:
return Nil;
break;
}
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:
return aOpen? 3:0;
break;
case 1:
return bOpen? 4:0;
break;
case 2:
return cOpen? 5:0;
break;
default:
return 0;
break;
}

}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.contentView.backgroundColor = [UIColor grayColor];
cell.contentView.alpha = .4;
}

if ([selectedIndexPath isEqual:indexPath]) {//if it was already selected
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 74;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;

}

//for high visibility purpose
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == 2) {
UIView *view = [[UIView alloc] init];
return view;
}

return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
if (section == 2) {
return 1;
}
return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

selectedIndexPath = indexPath;
[tableView reloadData];
}

#pragma mark - header delegate handler
- (void) headerTappedAtSection: (int) section {
BOOL openAction = false;
int numberOfRow;
switch (section) {
case 0:
aOpen = !aOpen;
openAction = aOpen;
numberOfRow = 3;
break;
case 1:
bOpen = !bOpen;
openAction = bOpen;
numberOfRow = 4;
break;
case 2:
cOpen = !cOpen;
openAction = cOpen;
numberOfRow = 5;
break;
default:
break;
}
[_tableView reloadSections: [NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
if (openAction && numberOfRow) {
[_tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
@end

最佳答案

我建议执行以下操作:

从 tableView 中移除:cellForRowAtIndexPath

if ([selectedIndexPath isEqual:indexPath]) {//if it was already selected
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

并在 reloadSection 之后添加内部 header 委托(delegate)处理函数(可能在 if (openAction && numberOfRow){} 中)

     [_tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

这将确保在重新加载部分后选择单元格。由于保存了 selectedCellIndex,您应该能够使用它来选择单元格。

关于iphone - UITableView 在 cellForRow 中使用 selectRowAtIndexPath 导致双高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13273719/

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