- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景:我正在尝试实现一个可以折叠和展开的分段表格 View 。折叠时,表格会记住选择的内容并在展开时保留选择状态。
问题:a 部分已扩展并正常运行。该单元格已按应有的方式被选中。但是,位于同一位置但下一节的单元格似乎也突出显示了。
示例:我有 2 个部分:A 和 B。在每个部分中,我有 3 个单元格:a、b 和 c。我展开这两个部分,以便所有单元格都显示在屏幕上并且没有选择任何内容。然后我点击 (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/
我有一个类,允许用户将条目添加到服务器端表。一切正常,直到我尝试刷新 UITableView与新数据。我调用服务器来获取新数据集,用它来刷新 NSArray那是表的数据源,然后尝试重新加载表。这是从服
我正在尝试以编程方式选择 TableView 行。我一直在关注此处的文档和其他引用资料,但它不起作用。我没有收到错误,只是没有选择行。 注意:目前我只是尝试手动选择一行。 我是否正确使用了委托(del
我觉得这是一个需要特别注意的问题......我的Google-fu很好,但我还没有得到任何有用的东西。 这是最简单的事情,但我似乎无法弄清楚它的问题所在。 我有一个 UITableView。这是我的
我用 [tableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionMiddle
I've got a menu that's a UITableview in a UIPopovercontroller that when selected scrolls the parent
我有大约 80 件元素。如果我选择的索引是 15(或更小的值),它会滚动到正确的位置。但如果选择索引为 70,则它不会滚动。但是当我手动滚动时它选择了该行。有谁知道如何解决这个问题? -(void)s
我正在尝试使用 selectRowAtIndexPath 强制选择(突出显示)UITableViewCell.. 例如,可以说我希望始终突出显示 TableView 中的第一个单元格,我在 viewD
我有以下方法来选择当前中心 View Controller 的行: - (void)_updateSelection { [self.tableView reloadData]; [_
我想调用 selectRowAtIndexPath 在作为 UIViewController 的 subview 的 UITableView 上,我从中调用它。 我已经设置了它,这样当你选择一个单元格
我有一个 UITableViewCell 的子类,它给我自定义样式和额外数据。 当用户交互时一切正常,但是当我通过调用 selectRowAtIndexPath 以编程方式选择它时,它会恢复为标准选择
我正在尝试使用以下代码以编程方式选择 TableView 中的所有行: func tableView(tableView: UITableView, cellForRowAtIndexPath in
背景:我正在尝试实现一个可以折叠和展开的分段表格 View 。折叠时,表格会记住选择的内容并在展开时保留选择状态。 问题:a 部分已扩展并正常运行。该单元格已按应有的方式被选中。但是,位于同一位置但下
我有一个 UITableViewController,它根据用户之前的选择动态填充。在某些情况下,用户应该能够选择表中的多行,但在其他情况下他不应该。在这两种情况下,我都想自动选择第一行,但我发现只有
我经常需要将表格 View 单元格设置为初始选定状态,对此我使用以下代码: [self.tableView selectRowAtIndexPath:indexPath animated:NO
我正在尝试以编程方式在表格 View 中选择一个单元格。我在点击 selectRowAtIndexPath 时收到 SIGABORT 错误。 myIndex 的值为 1。我从已实现所需的 tablev
我正在制作自定义 UITableView 菜单选择器组件。每次我选择特定的行时,我都会保存该行的索引路径,这样下次用户选择另一行时,人们就可以知道他之前选择的行。所以我将其添加到 cellForRow
我在 tableView:didSelectRowAtIndexPath: 中有以下代码 [tableView reloadData]; [tableView selectRowAtIndexPath
我是一名优秀的程序员,十分优秀!