Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust the left margin for prototype cells in a UITableView?

If I create a UITableViewController, for example via File → New Project... → iOS → Master-Detail Application in Xcode, a UITableView is created with a prototype cell.

The generated view hierarchy is:

UITableViewController view hierarchy

A left "margin" is automagically created between the Cell's Content UIView left edge and the "Title" text's UILabel element as shown below in orange.

Prototype cell gap

This results in a corresponding margin between the device's screen edge and the UILabel text at runtime:

Runtime gap

So, where is the width of this gap set, and how can it be adjusted?

The controls in the Size Inspector for the UILabel are greyed out:

enter image description here

My preferred option would to be able to set the width of this gap from within Interface Builder, but I would also like to understand where this gap is being set, and how to alter it programmatically.

like image 727
j b Avatar asked Sep 29 '14 19:09

j b


People also ask

How do I add a prototype cell to UITableView?

Choose iOS -> Source -> Cocoa Touch Class. Name the class TableCiewController and make it a subclass of UITableViewController. Go to the TableViewController. swift file and declare the following array.

How do you add a space between UITableView cells?

The way I achieve adding spacing between cells is to make numberOfSections = "Your array count" and make each section contains only one row. And then define headerView and its height. This works great.

How can we use a reusable cell in UITableView?

For performance reasons, a table view's data source should generally reuse UITableViewCell objects when it assigns cells to rows in its tableView(_:cellForRowAt:) method. A table view maintains a queue or list of UITableViewCell objects that the data source has marked for reuse.


1 Answers

You just need to set contentInset property of the table view. You can set value according to your need.

self.tableView.contentInset = UIEdgeInsetsMake(0, -15, 0, 0);

OUTPUT RESULT

like image 72
MilanPanchal Avatar answered Sep 19 '22 13:09

MilanPanchal