UITableView - Keep bottom-most cell scrolled to th

2020-05-05 01:58发布

问题:

Im creating a music based app. The playlist screen is a UITableView. It has 3 sections: Previous Tracks, Now Playing, Next Tracks. They are in order as follows:

-- PREVIOUS TRACKS SECTION --
   Track Z
   Track Y
   Track X
-- NOW PLAYING SECTION --
   Track A
-- NEXT TRACKS --
   Track B
   Track C
   Track D

I always want the 'Now Playing' section to be scrolled to the top of the table view. This is not a problem if: A) There are no items in the Previous tracks section or B) There are enough tracks in the Next Tracks section to pass beyond the bottom of the UITableView.

However, if there are, for example 10 tracks in the Previous Tracks section, one in the Currently Playing and 2 in the Next Track section, then when I tell the table view to scroll so the Now Playing section is at the top of the view, it just springs back so it is 3 items from the bottom of the view (the 2 'Next Track' items plus the Currently Playing).

To put it another way, look at your iPod library or any TableView on the iPhone. What im trying to acheive is to scroll the last track in the list to the top of the screen and leave blank white space underneath.

Is there any way of telling the TableView not to spring back into place in this scenario? I have tried playing a large height cell there, but its hacky and causes problems when re-arranging cells in the TableView.

Technically this is Monotouch, but im fine with Objective C, so whatever language is fine.

回答1:

Look at the contentInset property which the table view inherits. You will have to set the bottom inset just so that the bottom most cell is visible.

Example:

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, self.tableView.frame.size.height - 88, 0);


回答2:

I had the same problem, but The issue was in updating/reloading the UITableView from background thread. Just in case if someone else have the same problem as me.



回答3:

Do not overwrite the inset value somewhere else, as in my case;)