UITableView在重新加载时向上滚动

时间:2014-06-16 10:54:49

标签: ios iphone objective-c uitableview tableview

我是stackoverflow的新手,对 iOS 开发相对较新。关于UITableView的重新加载,我遇到了一个让我疯狂的问题。在选择UITableView的任何行时,表格视图会向上移动一点。我已经在didSelect上重新加载了表格视图,即用户点击UITableViewCell时。我使用 自动布局 来计算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];
    }     
}

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

4 个答案:

答案 0 :(得分:0)

如果您的单元格是固定高度,那么您不需要实现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;
   }
}

答案 1 :(得分:0)

在重新加载表格之前添加此内容。

[self.tbl_search_details setNeedsDisplay];

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

我遇到了类似的问题。但是,我在我的原型单元格中的标签上使用约束,在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你有没有设法解决问题?

相关问题