2

I'm working on an a game app in swift that currently has a tableView displaying the scores, number of trys etc, downloaded in an array from parse.

However, this table can get pretty long if the user plays the game many times. So I'd like to improve the app by displaying the first, say, 20 objects of the array in a tableview, then, once the user scrolls down to the end of the table it automatically adds more rows and displays the next 20 objects in the array (along with the original 20, making the tableview now 40 rows)

If anybody's familiar with the twitter app, that's exactly what I'd like to go for. There's a set amount of tweets shown initially, then once you scroll down to the end of the table more tweets are loaded, in order to decrease loading times.

Problem is, I really have no clue how to implement this at all. I've never been in the situation where I only need to use part of an array. Any help would be greatly appreciated. Dan

rici
  • 234,347
  • 28
  • 237
  • 341
dan martin
  • 1,307
  • 3
  • 15
  • 29
  • Downvote is probably because this is too wide-ranging. Ask a specific UI question such as what I answered below, or ask a specific model-manipulation question. – BaseZen Jul 31 '15 at 00:07
  • UiTableView is a virtual list and it calls you back for the data as it comes into view (or ahead). You just answer the callbacks and it handles and loading partial data (and destroying rows out of view). So, the answer to this question is you shouldn't have to .... right? – bryanmac Jul 31 '15 at 00:16
  • No. Read my comments to @bryanmac's answer. – BaseZen Jul 31 '15 at 21:50
  • Actually, that is how the datasource callbacks work on a UITableView - it keeps memory low by calling back for data and if you re-use cells it keeps memory low. – bryanmac Aug 01 '15 at 02:22

2 Answers2

0

UITableView is a virtual view where it calls you back for the data to create cells for a given row that's in view and destroy's cells that go out of view. You should also re-use cells that have been created.

So, I think the answer to your question about pre-loading portions of the list is ... you shouldn't have to. Just implement the data source and answer the data call backs.

More here in the apple docs on the datasource for a UITableView

bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • But there's a reason Twitter & FaceBook work the way they do...the backing data is huge and is impractical to represent in device memory...so the backing data must be demand-loaded. Presumably this is what the OP ultimately intends to do. – BaseZen Jul 31 '15 at 00:27
  • The datasource interface with callback does not load all the data in memory. It's a virtual callback mechanism - as cells come into view, it calls you back for data at row x. As data scrolls out of view, it destroys the cell. That's why it's a row by row call back mechanism. I've implemented it backed virtually by 100s of thousands of items and kept memory low. That's also why they tell you to re-use cells that go out of view. Did you read the link I supplied? – bryanmac Aug 01 '15 at 02:19
-2

This is addressed in How to know when UITableView did scroll to bottom in iPhone in Objective-C. If any help needed translating this to Swift, post as a comment and I'll update answer.

EDIT -- The real question:

The tableView delegate methods allow you to perform arbitrary logic / mapping between your data and the table's viewable data. So when you do detect a scroll to the bottom, increment an internal state variable, such as var rowsToReveal declared as a class-wide stored property. After incrementing, call tableView.reloadData(). Then re-write numberOfRowsInSection delegate method to use rowsToReveal rather than someDataArray.count or whatever hardcoded value you had. Make sure rowsToReveal never exceeds the count, of course, otherwise -- instant crash.

Community
  • 1
  • 1
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • 1
    Not quite. The link was useful and I thank you for that, but the more pressing part of my question that was highlighted was how to only display the first x objects of an array in a tableView. Meaning if I have an array of 500 objects, how do I only display the first 20, and then increase the rows of the tableview on scroll down to the bottom. The link merely answers how to know when the user has scrolled down to the bottom. – dan martin Jul 31 '15 at 00:13