The Cook
The Cook

Reputation: 1443

How to determine the reason of low performance of a UITableView

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

Answers (1)

Rohit Pradhan
Rohit Pradhan

Reputation: 3875

  • As you fetching data from server for each cell it will always lag.For making the experience smoother you need fetch 10-15 JSON object in advance & store them in the array. Whenever you make new request add those objects in the array.
  • So you will not make service request for each cell instead you make request after 10-15 cell(you can increase this count if your json object contains less fields). You can show spinner at bottom when make new request.
  • For this also need to make mechanism at server side to give you object in array of 10-15

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

Related Questions