Quantcast
Channel: Yudiz Solutions Ltd.
Viewing all articles
Browse latest Browse all 595

TableView Optimisation

$
0
0

Introductions

Click To Download Full Source Code

We all know that what is UITableView and how its imported to iOS application development.we also know the what is cell reuse identifier and also already use that one.

But still we need to know about how to optimise tableview. i am listed below some important method and standard to tableview optimisations.

1. Reuse Cell Identifier

Reuse cell instances for specific type of cell you should have only one instance of cell.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
	let cell = tableView.dequeueReusableCell(withIdentifier: "userCell") as! UserCell
	return cell
}

2. Data Binding

But very important tableView(UITableView, cellForRowAt: IndexPath)method, which should be implemented in the dataSource of TableView, called for each cell and should work fast. So you must return reused cell instance as quickly as possible.

Don’t perform data binding at tableView(UITableView, cellForRowAt: IndexPath) method. because there’s no cell on screen yet.

Just get the reuse cell from catch or create new and return immediately. this increase calling frequency of this method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "userCell") as! UserCell
     return cell

}

data-binding

For data binding you can use tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath)method which can be implemented in the delegate of UITableView. This method called before showing cell on screen.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

	if let userCell = cell as? UserCell {

		let dict = arrName[indexPath.row]
		userCell.lblName.text = dict["name"]
		userCell.lblPost.text = dict["post"]
		userCell.btnFollow.isSelected = dict["isFollow"] == "1"
		userCell.imgProfile.image = UIImage(named:dict["img"]!)

	}
}

data-binding

3. Height of Cell

As we know UITableView is just child of UIScrollView. So how TableView know about its contentSize?. its calculating all cell heights and summing it.

The tableView(UITableView, heightForRowAt: IndexPath) method is called for each cell even if is display or not. you need to return height very fast.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

}

Since iOS 8, we can use automatic height calculation without implementing tableView(UITableView, heightForRowAt: IndexPath) method. we can user tableView.rowHeight properly.

3.Height of Cell

4. Other Point need to Know

AutoLayout is really so slow

The reason of relative low performance of Auto-layout in constraint solving system. More subviews you have to layout and constraints you have to solve, more time you spend for returning cell to the UITableView. performing some base math calculations with small number of values.

  • Load images at background, round their corners at the same place, and then assign processed image to UIImageView.
  • Catching images to local. do not load every from network.
  • Do not load heigh resolution image. only respectively to UIImageView size.
  • Perform operations to background thread and refresh displayed content on the main thread.

Click To Download Full Source Code

Yogesh Makwana

Yogesh Makwana | iOS Developer

I am an iOS developer at Yudiz Solutions Pvt. Ltd. - a leading iPhone App development company. My life motto is 'Do my best, so that I can't blame myself for anything.' I have a deep rooted obsession for developing creative iOS apps and different aspects.

Viewing all articles
Browse latest Browse all 595

Trending Articles