UITableView无法以编程方式正确滚动到底部

时间:2014-12-29 07:34:08

标签: ios objective-c uitableview

我有一个100行的表,想要以编程方式将其滚动到任何元素并选择该元素。在第一个控制器中,我指定行和部分,并使用UITableView将控制器推入导航堆栈。在“tableView-controller”的viewWillAppear中,我使用代码:

[tableView_ selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];

奇怪的是,它完美地滚动到99个第一行中的任何一行,第100行被选中,但仍然在可见区域“下方”。我必须手动滚动表格以显示它。

如果我将该代码放在viewDidAppear中,表格滚动得很好,但是在明显的延迟之后。

我还尝试了什么:

  1. 降低tableView的高度。我得到了tableView,它部分覆盖了最后一行模糊的屏幕。
  2. 使用contentSize - 将其高度设置为小的大值 - 没有运气。
  3. 设置contentInset.bottom - 工作,但我底部有一个无用的空间。
  4. viewDidAppear中,像往常一样处理99行,并为第100行设置自定义contentOffset

    offset.y += tableView_.contentSize.height - tableView_.bounds.size.height + 44;
    

    也工作,但似乎是一种丑陋的方式。

  5. 那么,当“tableView-controller”出现时,我应该怎样做才能让我的tableView预先滚动到第100个元素?

    更新。 我不使用任何xib或故事板,只是在viewDidLoad中定义视图:

    - (void)viewDidLoad {
      [super viewDidLoad];
    
      tableView_ = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
      tableView_.dataSource = self;
      tableView_.delegate = self;
      tableView_.sectionFooterHeight = 0.0f;
    
      self.view = tableView_;
    
      numbers = [NSMutableDictionary dictionary];
      numberSectionTitles = [NSMutableArray array];
    
      //... some model initialization
    
      headerView = [[UIView alloc] initWithFrame:(CGRect){0, 0, 320, 60}];
      headerView.backgroundColor = [UIColor greenColor];
    
      UILabel *l = [[UILabel alloc] initWithFrame:(CGRect){20, 20, 280, 20}];
      l.textColor =[UIColor whiteColor];
      l.tag = 121;
    
      [headerView addSubview:l];
    }
    

    并在viewDidAppear(脏解决方法)中调整选择

    -(void)viewDidAppear:(BOOL)animated
    {
      [super viewDidAppear:animated];
        int row = _selectedIndex  % 10;
        int section = (_selectedIndex - row) / 10;
    
      if(row == 9 && section == 9){
        CGSize contentSize = tableView_.contentSize;
        CGPoint offset = tableView_.contentOffset;
    
        offset.y += tableView_.contentSize.height - tableView_.bounds.size.height + 44;
        tableView_.contentOffset = offset;
      }
      [tableView_ selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    
    }
    

3 个答案:

答案 0 :(得分:2)

可能是您的视图高度与tableview不同。你的superview(你要添加你的tableview)应该是相同的。你的最后一行是隐藏的,因为你的视图与你的桌面视图的高度不同,这可能会解决你的问题。

答案 1 :(得分:1)

您可以手动执行此操作:

[_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section] animated:NO scrollPosition:UITableViewScrollPositionNone];

CGRect rect = [_tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];

CGFloat offsetY = rect.origin.y+rect.size.height/2-_tableView.frame.size.height/2-_tableView.contentInset.top/2;
if (offsetY < -_tableView.contentInset.top)
    offsetY = -_tableView.contentInset.top;
else if (offsetY > _tableView.contentSize.height-_tableView.frame.size.height)
    offsetY = _tableView.contentSize.height-_tableView.frame.size.height;

_tableView.contentOffset = CGPointMake(_tableView.contentOffset.x, offsetY);

答案 2 :(得分:0)

if(cell == nil)

如果您在CellforRowatIndexpath中编写了这样的if语句,请删除该特定if语句并继续。那条线导致问题

享受编码

相关问题