Andrey Solovyov
Andrey Solovyov

Reputation: 1063

UITableView does not PROPERLY scroll to bottom programmatically

I have a 100-row table and want to scroll it programmatically to any element and select that element. In first controller I specify row and section and push the controller with the UITableView onto navigation stack. In viewWillAppear of "tableView-controller" I use the code:

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

The curious part is, it scrolls perfectly to any of 99 first rows, 100-th row gets selected, but remains "below" the visible area. I have to scroll the table manually to reveal it.

If I place that code in viewDidAppear, table scrolls nicely, but after a noticeable delay.

What did I also try:

  1. Reducing the height of tableView. I got tableView which partly covers the screen with last row obscured indeed.
  2. Playing with contentSize - setting its height to small large values - no luck.
  3. Setting contentInset.bottom - worked, but I've got a useless space at the bottom.
  4. In viewDidAppear, processing 99 rows as usual and setting custom contentOffset for the 100-th row:

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

    worked too, but it seems to be an ugly way.

So, what should I do to get my tableView pre-scrolled even to 100-th element when "tableView-controller" appears?

UPDATE. I do not use any xibs or storyboards, just defining views in 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];
}

And adjust selection in viewDidAppear (dirty workarounds)

-(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];

}

Upvotes: 0

Views: 974

Answers (3)

Friend_LGA
Friend_LGA

Reputation: 71

You can do it manually:

[_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);

Upvotes: 1

vijeesh
vijeesh

Reputation: 1327

if (cell==nil)

if you've written an if statement like this in your CellforRowatIndexpath remove that particular if statement and proceed. That line causing problem

Enjoy coding

Upvotes: 0

Nitin
Nitin

Reputation: 451

may be your view height is not same of tableview. your superview(on which you are adding your tableview) should be same. your last row is hiding because your view is not as same height as your tableview,may be this solve your problem.

Upvotes: 2

Related Questions