Adding Margin to UITableViewCell

19,765

Solution 1

Leave everything as it is. Don't try to inset your whole TableView. Create a container View inside your TableViewCell instead:

enter image description here

Set the row height:

enter image description here

Also in code:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 160.0
}

Specify the right distance to the edges:

enter image description here

Now add the elements and specify the constraints as well:

enter image description here enter image description here enter image description here enter image description here

Don't forget to set the cellIdentifier:

enter image description here

The result in the simulator:

enter image description here

If you want to look at it under the hood: I've uploaded it as github project

Solution 2

I don't know if I understood your question. I thought that your question was to centre vertically those two rows on screen. my code do that.

Approach:

I usually play this by adding extra cell(s) at the start and/or end of the UITableView

#define cell_height 100

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return array.count + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row > 0)
        return cell_height;
    else
    {
        CGFloat tableHeight = tableview.frame.size.height;
        CGFloat contentHeight = array.count * cell_height;
        CGFloat whiteAreaHeight = tableHeight - contentHeight;
        if (whiteAreaHeight > 0)
            return whiteAreaHeight/2.0;
        else
            return 0;
    }
}

- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if (indexPath.row > 0)
    {
       NSInteger realArrayIndex = indexPath.row - 1;
       // your existing code here
       return cell;
    }
    else
    {
       //return empty cell. add it to the storyboard with different identifier and get it.
    }
}

I hope that helps!

Share:
19,765

Related videos on Youtube

Tonespy
Author by

Tonespy

Updated on September 15, 2022

Comments

  • Tonespy
    Tonespy over 1 year

    I am trying to achieve a view I mocked out on sketch. I've replicated it on Android cause I'm really good on that platform. I'm good on iOS, but the UI is kind of my weak point. The Image

    I extended a UIViewController and on my StoryBoard have the top to be a view and the bottom a tableview. The problem I'm having is centering the UITableViewCell to look like that in the app itself. Below is the solution I've tried. But, it just squeeze it all to the top. NB. I use UIView to draw those Tiny Lines in the UITableViewCell

    func configureTableView() {
        //First One I tried then later commented it out
        loanStateTable.rowHeight = UITableViewAutomaticDimension
        loanStateTable.scrollToNearestSelectedRowAtScrollPosition(UITableViewScrollPosition.Middle, animated: true)
    
        //Second One I tried
        var edgeInset = UIEdgeInsets(top: 16, left: 16, bottom: 0, right: 16)
        loanStateTable.contentInset = edgeInset
    }
    

    And the storyboard view UITableViewCell

    Any help would be appreciated. Thanks

    Output:

    OutPut Image

  • Tonespy
    Tonespy over 8 years
    I tried your answer out and it did worked out too...Thanks