简体   繁体   中英

How to get a padding inside TableView?

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

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

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;
}

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