1

I have a TableView with a dark transparent background which I'd like to have a padding I tried to accomplish that using contentInsets, contentSize and contentOffset:

CGFloat padding = 10.0f;
CGSize size = self.tableView.bounds.size;
size.width -= (padding*2);
size.height -= (padding*2);
self.tableView.contentSize = size;
self.tableView.contentInset = UIEdgeInsetsMake(padding, padding, padding, padding);
self.tableView.contentOffset = CGPointMake(-padding, -padding);

That doesn't work. I get the padding, but neither are the cells resized nor respositioned.

I know, I could nest the TableView inside another View, but there should be another solution.

EDIT:

I actually had a misconception as to the meaning of 'contentSize'. It's not the viewport's size but the actual size of, you name it, the content. So I probably will have to..

  1. resize the individual cells if possible (might collide with the TableView's layout process)
  2. apply a content offset to the TableView as above
roplacebo
  • 2,837
  • 2
  • 17
  • 19

2 Answers2

3

For width, you are going to have to mess with each cells frame. as for top and bottom inset, use setContentInset:

Kyle
  • 434
  • 2
  • 10
1

If you want a paffing for your tableview simply change its frame.

If you want a padding for your cells, try this code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

      static NSString *CellIdentifier = @"Custom";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
            CGRect cellFrame = CGRectMake(20,0,200,200); //CHANGE FRAME FOR YOUR APP
            UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:CellIdentifier] autorelease];

      }

          //INSERT YOUR CODE TO RETRIVE TEXT TO INSERT INTO A CELL

    return cell;
}
MaTTP
  • 953
  • 2
  • 12
  • 27