简体   繁体   中英

iOS UITableView is not scrolling after adding cells

I have a UITableView that represents playable rounds in a game. On first launch, only two cells are shown in the tableView. As the user beats levels, more and more rows are added to the tableView. I reloadData every time the tableView is displayed (which happens each time the user beats a level).

Once the tableView contains enough cells to require scrolling, the tableView will not scroll and where the user left it. It will scroll to reveal that the cells are down below, but when the user releases their finger, the tableView bounces back, not allowing the user to interact with those cells that require scrolling to see. After completing another level and returning back to the tableView, the view will scroll properly.

When the scrolling issue exists, it is as though the tableView does not realize that it is big enough to require scrolling, so it bounces back to its original position, despite the fact that there are cells that are displayed down below when the user attempts to scroll.

I do not implement the heightForRowAtIndexPath or heightForHeaderInSection methods in the tableView's delegate, so that should not be interfering. I have searched this site and Google for similar issues, and have tried a few things including setting the contentSize, but have been unsuccessful.

Is there any reason for this lack of scrolling? Or, more importantly, is there a way to fix it? Thanks!

EDIT: I see some comments asking about the frame. I logged the frame of the view the table is in, the frame of the tableView itself, and the tableView's content size in the viewDidAppear method of the view the table i in.

Here are the results when the view appears the time before scrolling is necessary. Scrolling behavior is as expected; the user can scroll past the visible area, and it bounces back.

view.frame: {{0, 20}, {320, 460}}
tableView.frame: {{0, 145}, {320, 315}}
tableView.contentSize: {320, 308}

Here are the results when the view appears when scrolling should be necessary. Scrolling will allow the cells below to appear, but when the user releases their finger, it bounces back when it should not.

view.frame: {{0, 20}, {320, 460}}
tableView.frame: {{0, 145}, {320, 315}}
tableView.contentSize: {320, 352}

Note that the contentSize.height did change by 44, as it should have after a new cell was added. The contentSize.height is now larger than tableView.frame.height, but the scrolling behavior does not reflect this.

The protocol UITableViewDelegate conforms to UIScrollViewDelegate, so all you need to do is to implement the methods -scrollViewWillBeginDragging and -scrollViewDidScroll directly in your UITableViewDelegate implementation and they will be called automatically if the implementation class is set as delegate to your UITableView.

So in those methods you can implement logic related to showing content of your tableView. Like suppose you said while scrolling it should show new cell ie the top most cell must hide at that time. You could write logic in above method as,

//set your content of tableview to show new cell each time.

OR for scroll direction , you have method - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView,

// you can set here start and end coordinates and change them accordingly when scroll decelerates.

Writing your code in such way will not make the scroll bounce back. And you will get perfect results. Hope this helps.

 self.tableview.scrollEnable=YES;//Paste this line where initialized UITableView.

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