objective-cuitableviewwidthgrouped-table

Make a UITableView that is not 100% width


I would like to make a grouped UITableView where the cells are not 100% width, this is because I want to show the background, and each cell will have rounded corners.

Sort of like this;

UITableView with less than 100% width

However, I'm not sure how to do this using Storyboard; or is it only something you can do in code?

Ideally, I'd like the whole area scrollable; but make the cells appear less than 100% width


Solution

  • Not sure if this is the best solution; the one I have so far is...

    In storyboard

    1. Create UITableView as normal as child of a UIView, set up delegates, data source as required
    2. Add a UITableViewCell to this
    3. Create my own custom subclass of UITableViewCell;
    - (void)setFrame:(CGRect)frame {
        frame.origin.x += kInset;
        frame.size.width -= 2 * kInset;
        [super setFrame:frame];
    }
    
    -(void) drawRect:(CGRect)rect {
        [self.layer setCornerRadius:kCornerRadius];
        [self.layer setMasksToBounds:YES];
    }
    

    The constants are just any number for now, say 10

    Next I ensure my UITableViewCell is pointing to this subclass.

    Now my cells appear with a margin and the uitableview itself is 100% width.

    I will have to keep playing around with it; maybe there is a better solution