简体   繁体   中英

UITableView won't scroll to certain position

[self.tableView setContentOffset:CGPointMake(0,48) animated:NO];

I have a UITableView that has aa UIView in the header. My UITableView won't scroll to (0,48) unless animated: YES. I don't want it to animate.

Anyone know whats up?

You can try the method named -scrollRectToVisible:animated .


I misunderstand your question. So, what you want is to initialize the tableview with a shift of y-coordinate about 48 pixels ?

This will make the tableView begin from yPos 48.

tableView.contentInset = UIEdgeInsetsMake(48, 0.0, 0.0, 0.0);

Hope this can help you....

To set the UIView for the section header, you're using function:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; 

I suspect you invoke setContentOffset somewhere in viewWillAppear, but that function is called later. And I think it overwrites your contentOffset. ...not sure why it doesn't when animated is YES, but anyway, just put your setContentOffset into this function like this:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = nil;
    if (section == SEC_WITH_IMAGE_HEADER) {
        headerView = self.neededView;
    }   
    [self.tableView setContentOffset:CGPointMake(0,48) animated:NO];
    return headerView;
}

Worked for me, hope so will for somebody else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM