TableView Optimisation

[vc_row full_width=”” parallax=”” parallax_image=”” bg_type=”” parallax_style=”” bg_image_new=”” layer_image=”” bg_image_repeat=”” bg_image_size=”” bg_cstm_size=”” bg_img_attach=”” parallax_sense=”” bg_image_posiiton=”” animation_direction=”” animation_repeat=”” video_url=”” video_url_2=”” u_video_url=”” video_opts=”” video_poster=”” u_start_time=”” u_stop_time=”” viewport_vdo=”” enable_controls=”” bg_override=”” disable_on_mobile_img_parallax=”” parallax_content=”” parallax_content_sense=”” fadeout_row=”” fadeout_start_effect=”” enable_overlay=”” overlay_color=”” overlay_pattern=”” overlay_pattern_opacity=”” overlay_pattern_size=”” overlay_pattern_attachment=”” multi_color_overlay=”” multi_color_overlay_opacity=”” seperator_enable=”” seperator_type=”” seperator_position=”” seperator_shape_size=”” seperator_svg_height=”” seperator_shape_background=”” seperator_shape_border=”” seperator_shape_border_color=”” seperator_shape_border_width=”” icon_type=”” icon=”” icon_size=”” icon_color=”” icon_style=”” icon_color_bg=”” icon_border_style=”” icon_color_border=”” icon_border_size=”” icon_border_radius=”” icon_border_spacing=”” icon_img=”” img_width=”” ult_hide_row=”” ult_hide_row_large_screen=”” ult_hide_row_desktop=”” ult_hide_row_tablet=”” ult_hide_row_tablet_small=”” ult_hide_row_mobile=”” ult_hide_row_mobile_large=””][vc_column width=”1/1"][vc_column_text css=”.vc_custom_1480999185477{margin-bottom: 25px !important;}”]

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
}

[/vc_column_text][vc_column_text css=”.vc_custom_1480654820138{margin-bottom: 25px !important;}”]

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
}
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"]!)
}
}
[/vc_column_text][vc_column_text css=".vc_custom_1480654922653{margin-bottom: 25px !important;}"]
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.
[/vc_column_text][vc_column_text css=".vc_custom_1480654979685{margin-bottom: 25px !important;}"]
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.
[/vc_column_text][/vc_column][/vc_row][vc_row video_opts="" multi_color_overlay="" css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