Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView ContentInsets scrolling content horizontally

i want some left space, 10 pixels, to UITablViewcCell. I write code below but its scrolling content horizontally. I dont want that.

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 130.0, 768.0)];
    self.horizontalTableView.contentInset = UIEdgeInsetsMake(0.0, 10.0, 0.0, 0.0);

Do i need to use contentOffset contentSize property of tableview.

like image 476
Ameet Dhas Avatar asked Feb 19 '13 06:02

Ameet Dhas


1 Answers

I think the best solution is to create subclass UITableViewCell and set the frames of its subiews in layoutSubviews method to suit your spacing requirements. This will be lot cleaner and extendible. A closer look at tableview cells is a good read for this.

But there are some short-cuts towards this as well, each with its own disadvantages:

  1. Set indentation for cells

    -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath

    This method will add indentation to your cell contents (left margins). But the cell separator will occupy the actual width of the cell, i.e the table width. So you'll need to set the separator style to none, so that it is won't be shown. But not applicable if you need a cell separator.

  2. Reduce the entire tableViews frame size

    This is the easiest way and quite obvious, but its lame when you require a margin between cell and the tableView.

As you have done it, setting contentInset, you should have noticed the discussion about it in Docs.

Use this property to add to the scrolling area around the content. The unit of size is points. The default value is UIEdgeInsetsZero.

I thought playing with contentOffset and contentSize would help achieve margins, but for me changing the values never had any effect. Probably as Andrey said, they might be managed internally. But I would love to hear more about these properties and their reflected behaviors.

EDIT

contentOffset and contentSize are inherited from UIScrollView and means same in UITableView as it a subclass of UIScrollView.

like image 79
egghese Avatar answered Sep 22 '22 07:09

egghese