简体   繁体   中英

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.

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, ie 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.

Another way that could work, is that, instead of setting the tableView frame, you can set the frame for cell's contentView , something like this,

- (void) layoutSubviews
{
    [super layoutSubviews];

    CGRect frame = self.contentView.frame;
    frame.size.width = frame.size.width * 0.9; //90% of total width
    frame.origin.x = self.center.x - (frame.size.width/2); //center the contentView
    self.contentView.frame = frame;
}

The using the above, u can add any left or right or left padding u need for the cell. and all the subViews will be added to the contentView , hence no problems there.

happy coding..!

Your description isn't quite clear to me, so I may be completely off here. But I'll take a guess, and also ask that you try to clarify more if it isn't right.

You set the contentInset of the tableView, and I'm guessing it worked to set a margin on the left side. But I think it caused the tableView to scroll because you didn't reduce the width of the inside view.

It seems there's strange bug in UITableView so edge insets works fine only for top and bottom edges. The best (but still ugly) solution is to shift content in your cells.

contentOffset and contentSize properties shouldn't be changed manually in tables, it's kinda internal things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM