Adding Margin to UITableViewCell

前端 未结 3 1953
失恋的感觉
失恋的感觉 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: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!

提交回复
热议问题