How to avoid scrolling a UITableView when adding new events? - iphone

I have a UITableView. It loads sets of events at regular time intervals. I want to be able to scrol the table view to see all events. I had inserted latest events at position number zero in a row. So whenever I reload the data, my table view has moved one row down. So the user can't view exactly what he wants to read. So I want to reload the UITableView without moving its scrolling position. How can I do that?
Note that my event text height should be vary based on events.

You can either use UITableView's -scrollToRowAtIndexPath:atScrollPosition:animated: or, probably better, UIScrollView's -setContentOffset:animated:, to reposition the table view. You'll probably just have to note the current offset, calculate the new offset (based on where & how many rows you added), and set it after you update the table.

Related

Reducing the amount of time that it takes to do date calculations

I am running the following code in cellForRow that does an on-the-fly date calculation. After seeing some stuttering whilst scrolling, I ran a Time Profiler while replicating the stuttering and determined that this bit of my code is the issue. As far as I am aware, this is the best way to accomplish getting a double for difference in number of days and then converting to number of years? Any suggestions on how to reduce the amount of time this takes to hopefully improve scrolling?
Provide default value and then use a completion handler to detect when the calculation is finished. then use reload rows at indexpath. That will let the user scroll and calculate the value in the background. And once the value is calculated, it will reload the cell to show the final value.
Date Formatters are always a heavy task to perform. If we are performing inside the table view cell then we will face scrolling issue with the table view. Approach for better performances using table view cell + Date formatters is to provide final data values to table view cell subview components. Data decoration should be done prior to assigning values to the table view cell.
Instead of using Dictionary use struct to provide data to cell. While initializing model classes all date formatters work should be done.
Now model class array will be passed to table view to render table.

Using xcode 6.4, Hide/Unhide rows in TableView sections when there are many sections

I have a Table with many sections and each section also has rows. How can I hide/unhide the rows per section? the table will be easier to manage if the user only sees sections and can 'tap' the section to see the row selections.
Implement tableView(_, numberOfRowsInSection) in the table view's data source to return either 0 (to hide them) or the actual value. Then, in the table view's delegate, implement tableView(_:viewForHeaderInSection:) to return a view that listens for taps.

How to make an never ending UITableView?

I have some options in an UITableView and want that the user can scroll forever. When the end is reached, I want to append the begginning so the user can continue to scroll.
How would I do that?
In tableView:numberOfRowsInSection: return some huge number, and act accordingly in tableView:cellForRowAtIndexPath: by returning same cells over and over.
You can also have limited number of rows, and at the end show a different cell - containing a button for user which he can use to append new rows. When that button is clicked you simply call [tableView reloadData] and provide more rows in table's data source.
Aren't you by any chance misusing the table-view for something it doesn't fit that well ? Maybe you would be better off using just a UIScrollView and reposition subviews as user scrolls over.
I think the only problem is how to trick in the method numberOfRows:. One possible solution is to return some big number here, for example, 10 000. Then, you set up a correct cellForRowAtIndexPath: to return based on your data's number modulo. For example, if you have 100 data entity, then you have to get the indexPath.row % 100

Selection reset issue when scrolling a UITableView

I have 15 rows in my table. When I select the first row and scroll to last row of the table, and come to back to the first row, my selection is reset. How can I fix this row selection reset issue when scrolling the table?
How are you selecting the row? Because of the table-cell reuse mechanism, just setting the selected property on an individual cell usually won't work; calling -selectRowsAtIndexPaths:animated: on the table view itself is the correct way to set its selection.

UITableView reusable cells problem

I have a UITableView and its data gets refreshed from a server connection. For the UITableViewcell, I have layout some labels across the row. So it can consider as a grid with UILabels(total no of labels = table rows * table columns).
When the data gets refreshed I have added an animation for the UILabel to highlight the table cell update.(Labels background color changes for about 0.5 seconds and restore to the previous background color). There I compare the old value with new value and I added the animation with comparing those. It works fine.
My problem is when I scroll the table, because of the reusable cells the labels are getting animated.(although the corresponding values are not change). Anyone have an idea to how to avoid this issue? thanks.
It sounds like you're calling reloadData and animating in tableView:cellForRowAtIndexPath:. Don't do this. There's no guarantee that UITableView will reuse the same cell (it sounds like it does, but it's unwise to rely on this, and I'm pretty sure it doesn't for reloadRowsAtIndexPath:withRowAnimation:). It will do the wrong thing if you remove a row, for example.
You can use -[UITableView visibleCells] to get an array of the currently visible cells. Loop over the array and use a custom method to tell each cell to reload with animation.