I’ve been trying to work out how to use table views and I’m a little stuck if I’m honest. I wanted to use a tableview with a limited number of rows (say 50 max). It starts of empty, with 0 rows. Then I wanted to do something along the lines of:
[self logMessage:@"Waiting for response"];
Which inserts a new row at the bottom with the above text. If I do another call to this pseudo function:
[self logMessage:@"Server response received"];
It should insert yet another new row below the previous row, and ensure it is visible. Once the above limit of 50 is reached, and a new message is inserted, I wanted the oldest message to be removed. All of this would be scrollable, with the latest being visible by default.
Am I looking at the right thing to do this? Eventually, I was hoping to have this in a nice little drawer below the main window, which I can then toggle from the main menu if needed. But as I said, I can’t work out how to use a table view properly, it doesn’t seem to be as straight forward as other objects are.
Any example code would be greatly appreciated!
Since log viewer is a read-only application of a
UITableView, the way you do it is rather straightforward once you understand the basics. Recall that table views rely on their data models to provide them with the correct information that needs to be displayed.A data model for “the last fifty lines of log” could be as simple as an
NSMutableArray: useinsertObject:atIndex:to add lines, andremoveLastObjectto remove the “overflow” lines, like this:Now you can use
logLinesas your table’s “model”: the data provider can tell how many lines there are by looking atlogLines.count; the content of each row in the table will be the object at the corresponding index inlogLines, and so on. Take a look at theUITableViewsection of your favorite iOs tutorial for the “boilerplate code” that needs to be written in order to display array elements in aUITableView.