【问题标题】:UITableView scrolls up on reloadUITableView 在重新加载时向上滚动
【发布时间】:2014-08-06 04:23:31
【问题描述】:

我是 stackoverflow 的新手,对 iOS 开发比较陌生。我对UITableView 的重新加载有疑问,这让我发疯。在选择UITableView 的任何行时,表格视图会向上移动一点。我已经在didSelect 上重新加载了表格视图,即每当用户点击UITableViewCell 时。我使用 autolayout 来计算 UITableViewCell 的行高。但是,每当单击表格视图行时,表格视图都会滚动一点,即表格视图内容会突然向上移动。这是我的代码..

#pragma mark UITableView DELEGATES AND DATASOURCE

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;//enter code here
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arr_display count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //-----COMMENTED AUTOLAYOUT OUT TO TEST------HEIGHT IS KEPT STATICto 40-----

    [self configureCell:_stubCell atIndexPath:indexPath];
    [_stubCell layoutSubviews];

    // [_stubCell needsUpdateConstraints];
    // [_stubCell setNeedsLayout];

    CGFloat height = [_stubCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return height + 1;

    //setNeedsUpdateConstraints

    // return 40;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40.f;
}

- (void)configureCell:(cell_detail_search *)cell atIndexPath:(NSIndexPath *)indexPath
{
    //cell.lbl.text = _tableData[indexPath.row % _tableData.count];
    // NSDictionary *dict=[[[[self.arr_display objectAtIndex:indexPath.row] objectAtIndex:1] valueForKey:@"info"] objectAtIndex:0];

    cell.lbl_skills.text=[NSString stringWithFormat:@"%@",[self.arr_display objectAtIndex:indexPath.row]];

    if ([self.arr_selected_search_list containsObject:[NSString stringWithFormat:@"%@",[self.arr_display objectAtIndex:indexPath.row]]])//-----GIVING TICK TO THE CELL
    {
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType=UITableViewCellAccessoryNone;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell_detail_search *cell = [tableView1 dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    if (cell==nil)
    {
        cell =[[cell_detail_search alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.arr_selected_search_list containsObject:[self.arr_display  objectAtIndex:indexPath.row] ])
    {
        // [self.arr_id_list removeObjectAtIndex:indexPath.row];
        [self.arr_selected_search_list removeObjectAtIndex:[self.arr_selected_search_list indexOfObject:[self.arr_display objectAtIndex:indexPath.row] ]];

        self.str_comma_separated_selected_search_list=[self get_comma_separated_string_from_array:self.arr_selected_search_list];
    }
    else
    {
        [self.arr_selected_search_list addObject:[self.arr_display  objectAtIndex:indexPath.row] ];

        self.str_comma_separated_selected_search_list=[self get_comma_separated_string_from_array:self.arr_selected_search_list];

        NSLog(@"self.arr_id_list =%@",self.arr_selected_search_list);
    }

    [self.tbl_search_details reloadData];
}

#pragma mark scrollview delegate made for tableview pagination---------

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if ([str_heading isEqualToString:@"Skills"])
    {
        float diff=self.tbl_search_details.contentSize.height-self.tbl_search_details.frame.size.height;

    if (diff<0)
    {
        diff=0;
    }

    float drag_distance=self.tbl_search_details.contentOffset.y-diff;
    NSLog(@"drag distance %f",drag_distance);

    if (drag_distance>80)
    {
        [self.activity_pagination startAnimating];
        [self determining_the_api_to_call_with_str:self.str_heading];
    }     
}

如果有人可以帮助我,我将非常感激.....提前谢谢

【问题讨论】:

  • 看看你的代码,我明白你是如何努力寻找错误的:s

标签: ios iphone objective-c uitableview tableview


【解决方案1】:

如果您的单元格是固定高度,那么您不需要实现estimatedHeightForRowAtIndexPathheightForRowAtIndexPath - 您可以在 UITableView 定义中使用固定高度。

要在选择时更新“tick”附件,您可以直接更新配置,无需重新加载整个表格视图 -

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


    if([self.arr_selected_search_list containsObject:[self.arr_display  objectAtIndex:indexPath.row] ])
    {
        // [self.arr_id_list removeObjectAtIndex:indexPath.row];
        [self.arr_selected_search_list removeObjectAtIndex:[self.arr_selected_search_list indexOfObject:[self.arr_display objectAtIndex:indexPath.row] ]];

        self.str_comma_separated_selected_search_list=[self get_comma_separated_string_from_array:self.arr_selected_search_list];

         }

    else
    {



        [self.arr_selected_search_list addObject:[self.arr_display  objectAtIndex:indexPath.row] ];

        self.str_comma_separated_selected_search_list=[self get_comma_separated_string_from_array:self.arr_selected_search_list];

        NSLog(@"self.arr_id_list =%@",self.arr_selected_search_list);
    }

    cell_detail_search *cell=[tableView cellForRowAtIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];



}

另外,作为一种风格,类名应该以大写字母开头,所以你的单元格类应该是Cell_detail_search,或者更好的是DetailSearchCell

最后,如果你用 NSMutableIndexSet 替换你的 arr_selected_search_list 数组,你可以简化你的 didSelectRowAtIndexPathconfigureCell 方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([self.selectedIndexSet containsIndex:indexPath.row])
    {
        [self.selectedIndexSet removeIndex:indexPath.row]; 
    }  
    else
    {
        [self.selectedIndexSet addIndex:indexPath.row]
    }

    cell_detail_search *cell=[tableView cellForRowAtIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];   
}

- (void)configureCell:(cell_detail_search *)cell atIndexPath:(NSIndexPath *)indexPath
{
   cell.lbl_skills.text=[NSString stringWithFormat:@"%@",[self.arr_display objectAtIndex:indexPath.row]];

   if ([self.selectedIndexSet containsIndex:indexPath.row])
   {
    cell.accessoryType=UITableViewCellAccessoryCheckmark;
   }
   else
   {
    cell.accessoryType=UITableViewCellAccessoryNone;
   }
}

【讨论】:

  • 好点虽然......但这并不能解决我的问题......至于为什么表格向上滚动确实选择......它与自动布局有关......还是什么可能是原因....
  • 我不知道 - 我认为这与行高和整个表格的重新加载有关。也许删除 scrollViewDidEndDragging 的东西
  • 我可以通过您的方式实现我的结果,而无需重新加载表格。你拯救了我的一天。谢谢!!...仍然想知道为什么表格会在重新加载时向上滚动..我从未见过这种行为....你知道吗...
  • 另一件事,如果我删除 tableview 高度委托中的自动布局部分....然后 tableview 变得正常。所以它必须对自动布局做一些事情..但不能真正清楚地知道如何自动布局可能会影响这里..
  • 我从来没有在表格单元格中使用过自动布局,但是如果没有看到您的限制,很难说问题可能是什么
【解决方案2】:

在重新加载表格之前添加它。

[self.tbl_search_details setNeedsDisplay];

【讨论】:

    【解决方案3】:

    问题的一个可能原因是受委托人

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 40.0f;
    }
    

    我原来的行高返回 80,但这里给出的是 40.. 不知何故,桌子因为这个而猛拉,虽然它不应该是..当我故意在估计的高度部分返回 80 时,问题就解决了......有没有人有过这种经历?

    【讨论】:

      【解决方案4】:

      我遇到了类似的问题。但是,我在原型单元格中的标签和 viewDidLoad 中的以下两行中使用了约束:

      tableView.estimatedRowHeight = 80
      tableView.rowHeight = UITableViewAutomaticDimension
      

      这可确保我的单元格自动调整大小以显示其所有内容 - 这些内容可能因单元格而异(我按照此页面上的步骤 http://www.appcoda.com/self-sizing-cells/ 来实现此目的)。

      为了解决 tableView 滚动的问题,我首先将 tableView 的estimatedRowHeight 更改为它的当前值 80(之前是 66),当我重新加载选定的单元格时,这会阻止表格向上滚动。此外,在我的 tableView 中重新加载数据后(我使用 reloadRowsAtIndexPaths 方法),我调用它的 scrollToRowAtIndexPath 方法。例如:

      tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
      

      这会滚动 tableView 以显示所选单元格的所有内容。当我选择 tableView 底部的一个单元格时,我不再遇到 tableView 向上滚动的问题。

      @user3744496 你有没有设法解决你的问题?

      【讨论】:

      • 如果您还有其他问题,请点击按钮提问。
      • 好的,可以,我只是想看看 user3744496 有没有解决他的问题 :)
      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多