2016-06-24 75 views
0

我使用的tableview与autolayet角落设置为(0,0,0,0)。我有一个自定义单元格设置在表view.my问题是scrollToRowAtIndexPath不工作fine.my cellForRowAtIndexPath方法看起来像这样scrollToRowAtIndexPath无法正常工作?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    RTableViewCell *cell = (RTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell==nil) 
    { 
     for (UIView *currentView in tableView.subviews) { 
      if ([currentView isKindOfClass:[UIScrollView class]]) { 
       ((UIScrollView *)currentView).delaysContentTouches = NO; 
       break; 
      } 
     } 
     UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame]; 
     myBackView.backgroundColor =[UIColor colorWithRed:216/255.0 green:231/255.0 blue:244/255.0 alpha:1.0]; 
     cell.selectedBackgroundView = myBackView; 

    return cell;  
} 

我有动态数量的部分和动态数内的行。 在我viewDidAppear我打电话这样

dispatch_async(dispatch_get_main_queue(), ^{ 
    //int indexValue = (int)[sections indexOfObject:string]; 
    [rtable reloaddata]; 
     NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:0 inSection:6]; 
    [self.rtable scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
}); 

有人知道我要去的地方错了吗?

+0

什么是预期的结果,而是会发生什么? – Code

+0

我希望tableview在第0行的第6个部分,但它只是滚动到第3个部分。 – hacker

+0

你确定有足够的行允许滚动到positionTop的第6部分吗?您可能需要通过contentInset添加一些底部空间 – danh

回答

0

试试下面的代码viewWillAppearviewDidLayoutSubviews

[rtable reloaddata]; 

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 

     NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:0 inSection:6]; 
     [self.rtable scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 

    });