read

To configure tableView, you will typically set it’s properties in viewDidLoad.

To configure cell, you will typically set it’s properties in tableView:cellForRowAtIndexPath:, after dequeuing it of course.

Hide all separator lines

tableView.separatorStyle = .None
// Or
tableView.separatorColor = UIColor.clearColor()

This will hide all separator lines, including the top and bottom which bound the section. Read next section to hide only the lines between cells.

Hide separator lines betweeen cells

cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);

Hide separator lines for the empty cells

Somehow, table view will have separator lines in the empty space when there ain’t enough rows to fill a screen. This is a workaround.

# Set an empty footer
tableView.tableFooterView = UIView()

Full width separtor line

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0);

Make a cell unselectable

cell.selectionStyle = .None

Change cell selection background color

let bg = UIView()
bg.backgroundColor = UIColor.redColor()
cell.selectedBackgroundView = bg

Handling rotation

When device rotate, you should reload data so that the cells will reload.

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    tableView.reloadData()
}

Using custom cell in nib

A similar tutorial on using nib for UICollectionViewCell was written. UITable is smiliar.

tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "cell")

Highlighting cell

Cell is highlighted when a touch is held on it.

UITableView provides a default behaviour:

  1. It will change the cell.backgroundColor to gray
  2. It will change ALL subviews backgroundColor to clear

The result of (2) will cause your subview to “disappear”. The solution is to add another UIView/CALayer to your custom subview as a background.

If you don’t like gray, you can change the selectionStyle or selectedBackgroundView.

Table Section Header/Footer

Read this post on adding header/footer.

Table Header/Footer

This is for the table, not each section.

If you are not using autolayout, it is simply setting the tableHeaderView or tableFooterView.

It gets tricky if you are using autolayout, as iOS has not support well for it (yet)! Refer to this post.


Image

@samwize

¯\_(ツ)_/¯

Back to Home