2

I have got a TableView inside a Navigation Controller.

By default, the TableView has edge insets set on the left and on the right. That is, the content's width in the TableView is smaller than iPhone width.

My designer wants me to have the content stretched from left to right on the iPhone without any insets.

So, I tried the following code:

UIEdgeInsets inset = UIEdgeInsetsMake(0, -10, 0, 10);
tableView.contentInset = inset;

On the left, the content is now really at the iPhone left edge. Fine. It worked.

But on the right, no setting seems to move the content to the right.

Your help is much appreciated!

Thanks a lot.

user1006117
  • 431
  • 4
  • 16

1 Answers1

0

The Clean Solution would be

Subclass the UITableViewCell class, and override the layoutSubViews method like the following.

And also set tableView contentInsets to zero.

mTableView.contentInset = UIEdgeInsetsZero;

-(void)layoutSubviews {
   [super layoutSubviews];
   self.textLabel.frame = self.contentView.bounds;
}
chandu
  • 476
  • 5
  • 12
  • It does not work. The last value has no effect whatsoever. It does not change anything regardless of the value. – user1006117 Apr 09 '13 at 16:54
  • Thanks for updating the code. If I subclass the UITableViewCell class, do I have to put all the elements of my cell (custom ones) into it and then add the subclassed cell to the table view? – user1006117 Apr 13 '13 at 22:05
  • @user1006117 Let me know what are the other elements you need to implement on the TableViewCell. Yeah, if you need custom elements, you have to add them as subviews, and also updated their frames in LayoutSubViews method. – chandu Apr 15 '13 at 06:37
  • I have got an image on the left and an image on the right. Then I have color customization of the text and background. – user1006117 Apr 15 '13 at 17:24
  • @user1006117 So you are already using the TableViewCell subclass? If yes, just make sure your subViews frames are properly set. We can solve this quickly if you can reach me on my email. – chandu Apr 16 '13 at 07:53
  • no, at the moment I am just customizing the cell in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath – user1006117 Apr 17 '13 at 06:13
  • It does not work. In that method, I am now doing `tableView.contentInset = UIEdgeInsetsZero; cell.textLabel.frame = cell.contentView.bounds; cell.textLabel.frame = cell.contentView.bounds;` And it still simply does not work :) – user1006117 Apr 19 '13 at 18:24
  • Now that I created a custom cell and it still does not work :) – user1006117 Apr 20 '13 at 05:22
  • I think, the problem is nothing to do with tableview but probably with scrollview that is added automatically? – user1006117 Apr 20 '13 at 05:32
  • TableView is the subclass of ScrollView. It is working for me. Subclass the tableViewCell and override layoutsubviews – chandu Apr 21 '13 at 06:06