Adding Margin to UITableViewCell

前端 未结 3 1893
失恋的感觉
失恋的感觉 2021-02-03 15:08

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 poi

相关标签:
3条回答
  • 2021-02-03 15:14

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

    Set the row height:

    Also in code:

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

    Specify the right distance to the edges:

    Now add the elements and specify the constraints as well:

    Don't forget to set the cellIdentifier:

    The result in the simulator:

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

    0 讨论(0)
  • 2021-02-03 15:16

    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!

    0 讨论(0)
  • 2021-02-03 15:32

    From your screenshots it seems that you have a problem with the auto layout system and dynamic cell. For this is suggest to read this very good answer on stack overflow that explain how to build dynamic cells with auto layout. An important thing to consider is that creating views with auto layout is very fast and intuitive but it expensive from a performance point of view. This problem is accentuated for the UITableView where the scroll can result not smoothly. If you have simple cell (only few views and a view hierarchy with few levels) it can be ok but to the auto layout manager I suggest to:

    • specify a value for the property estimatedRowHeight if all the cells have the same height
    • implement the method tableView(_:estimatedHeightForRowAtIndexPath:) of the UITableViewDelegate if the cells have different heights.

    Regarding the problem with margins I suggest to nest a UIView inside the contentView of the cell and use it as container for all the other views of the cell. In this way you can add left and right margins between this view container and the contentView so that you can center it.

    0 讨论(0)
提交回复
热议问题