Reputation: 1443
I have a UITableView
as my main view of my application. The contents of this UITableView
is loaded from web via HTTP request as JSON, 5 item at a time. There is no performance issues with this request part.
After that, cells are generated programmatically according to JSON data's content. For example if JSON says a cell contains 1 UILabel
, 1 UITextField
and 1 UIButton
. I add these as subviews to correct UITableViewCell
. Initial state of the cells have no issues either.
As you scroll down, new items are loaded from web, and this is the method triggers "load more" functionality.
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let lastElement = data.jsonArray.count - 2
if indexPath.row == lastElement {
page += 1
getFeed()
}
}
However, as you scroll down you can notice small freezes and lags and this looks very bad. I have no idea why these small freezes occur.
This is how it looks right now. I recorded this on emulator, but on a real device it is much worse.
I don't know what should I do here. Any advices? Thanks.
Upvotes: 3
Views: 308
Reputation: 3875
This mechanism is also known as Pagination.
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ( (indexPath.row == array.count - 1)) {
// This is the last cell
[self loadMore];
}
}
// Load more
- (void)loadMore {
//make service request for next page (Make sure your response contains next 10 objects)
//on response add those objects in the array
//reload table view
[self.tableView reloadData];
}
Upvotes: 1