Howto fix TableView right margin? (swift)

2.4k views Asked by At

I've created a tableview within Viewcontroller and inserted disclosure indicator, but it is not showing, because the right margin is not correct.

What I tried: selecting table and click in xCode on "pin" button and set left and right to 0, select there all frames in container and clicked: add constraints

but without results.

How can i fix this? I attached the problem as image. (i have added a frame to focus the problem)

right margin

4

There are 4 answers

0
mostworld77 On BEST ANSWER

The problem was not the margin/layout.

To show disclosure indicator in tableview cell's, it is needed to select disclosure indicator from Accessory menu AND give a identifier for the cell. Without given identifier the disclosure indicator is not showing.

1
whereisleo On

Try to programmatically set the width and height of the tableView like so...

@IBOutlet weak var resultsTable: UITableView! // connect to tableView

override func viewDidLoad() {
  super.viewDidLoad()

  let theWidth = view.frame.size.width
  let theHeight = view.frame.size.height

  resultsTable.frame = CGRectMake(0,0, theWidth, theHeight)
}
0
Nakib On

You need to change seperator inset inorder to remove left margin. Its set to default change it to custom and make left insets and right insets to zero. You can set the 'Separator Inset' from the storyboard:

enter image description here

Change Seperator insets to Custom and make left to zero

enter image description here

0
Natasha On

Okay, there can be multiple reasons for which this can happen. For instance, one can forget to deck the cell with wrong identifier or may have set the accessory Indicator in a wrong way. My personal assumption is you probably have set constrain improperly.

So, I am just going to show you the whole process.

Step 1: Drag and UITableView inside your ViewController and drag a UITableViewCell inside that TableView. Select the Table View Cell and assign an identifier. Make sure, in your datasource method, -cellForRowAtIndexPath, you use this same identifier.

enter image description here

Step 2: Go to your View Controller and take an IBOutlet of a UITableView.

@interface MyViewController : UIViewController<UITableViewDataSource>

      @property(nonatomic, strong) IBOutlet UITableView *myTableView;

@end

Step 3: Go to the connection Inspector of your storyboard and connect the UITableView with this outlet.

Step 4: Go to the implementation file of your View Controller and populate the datasource methods. Also make sure, you set your TableView's datasource to your view controller.

enter image description here

Now when you build and run this code you won't see the accessory Indicator.

enter image description here

The reason is you didn't set the constraints.

Step 4: Just set the constrains like the following and you are good to go.

enter image description here

This is my final view.

enter image description here