iOS 7 UITableView 滚动后选择上一个 IndexPath

Posted

技术标签:

【中文标题】iOS 7 UITableView 滚动后选择上一个 IndexPath【英文标题】:iOS 7 UITableView selects previous IndexPath after scrolling 【发布时间】:2014-02-03 12:51:27 【问题描述】:

好的,所以我的应用程序中有这个令人费解的错误。 我动态创建了一个 TableView 并用一些数据填充它(数据不是这里的问题,空单元格的行为相同)。 UITableView 显示正常,数据加载正常。当我点击单元格中的行时,一切都很好,它会正常选择并正常取消选择!完全不用担心。

问题: 当我滚动UITableView 时,它会正常滚动,但是每当我在滚动后尝试选择一行时,都会选择错误的行。选择的行是开始滚动时指向的行。就像当您开始滚动时触摸时选择了该行,然后当您尝试点击表格中的另一行时触发didSelectRowAtIndexPath:。一旦选择了错误的行,您可以再次选择所需的行,直到您再次滚动!

可能是什么原因造成的?

这是我的表格视图代码!

//Function to display a summary view after answering all the questions
-(IBAction)displaySummaryView 
    viewSummary = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-60)];
    [viewSummary setAlpha:0.0f];
    [viewSummary setHidden:YES];
    UITableView *tableView = [[UITableView alloc] init];
    [tableView setBackgroundColor:[UIColor clearColor]];
    [tableView setSeparatorColor:[UIColor colorWithWhite:0.0f alpha:0.25f]];
    [tableView setDataSource:self];
    [tableView setDelegate:self];
    [tableView setTranslatesAutoresizingMaskIntoConstraints:NO]; //Taken care of elsewhere
    [viewSummary addSubview:tableView];

    //Get all the answers and sort them right!
    NSMutableString *predicate = [[NSMutableString alloc] initWithString:@"((SELF.id == '-1')"]; //Init with dummy
    for(Answer *ans in activeSession.answers) 
        [predicate appendFormat:@" OR (SELF.id == %@)", ans.question_id];
    
    [predicate appendFormat:@")"];
    summaryQuestionsAndAnswers = nil;
    summaryQuestionsAndAnswers = [currentQuestions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:predicate]];
    predicate = nil;


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    return [summaryQuestionsAndAnswers count];


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    return 1;


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    return 120.0f;


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    NSString *cellIdentifier = [NSString stringWithFormat:@"formCell%d",indexPath.row];
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) 
        //Cell creation here
    
    return cell;


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"Indexpath section %d row %d", indexPath.section, indexPath.row);


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
    [cell setBackgroundColor:[UIColor clearColor]];

【问题讨论】:

您是否在 UITableView 中使用自定义选择,您将在 cellForRowAtIndexPath 中管理选择: @NANNAV 你到底是什么意思?我应该从 cellForRowAtIndexPath 中处理选择吗? 是的,当我们使用 ReusableCellIdentifier 时,cell 出现在 tableView 中时会重用,显示您的选择和取消选择代码。 解决了,见下文。试图通过在 cellForRowAtIndexPath 中添加手势识别器来管理触摸,但这只会让事情变得更糟。不过感谢您的努力! 【参考方案1】:

解决了!

我有一个记录应用程序中所有触摸的窗口。这是搞砸了我的代码的功能!祝其他遇到此错误的人好运。这通常不是重用标识符问题。

if (touch.phase == UITouchPhaseBegan) 
    for (UIView *view in views) 
        if ( ![view isHidden] && [view pointInside:[touch locationInView:view] withEvent:event] ) 
            touchView = view;
            [touchView touchesBegan:[event allTouches] withEvent:event];
            break; //NOTE: this used to be a return in the previous version
        
    

【讨论】:

以上是关于iOS 7 UITableView 滚动后选择上一个 IndexPath的主要内容,如果未能解决你的问题,请参考以下文章

在 UITableView 中插入一些行后,它在 ios 7 中没有滚动

带有 uisearchDisplayController 的 UItableView 在 ios 7 中滚动时更改了 contentoffset

ios 7 uitableview 所选单元格在 beginupdates 后失去边框

设置子视图的layer.cornerRadius后iOS UITableView滚动不顺畅[重复]

IOS学习之UITableView滚动到指定位置

iOS 7 UITableView 使用透明的导航栏和工具栏