How to add Margins on UITableView to inset content

六眼飞鱼酱① 提交于 2019-12-23 05:04:56

问题


I have a table view and I want to include margins so that the table's content has some breathing room on the left and right, and also between cells.


回答1:


I managed to do a table view like:

My Storyboard design is like:

What I did was I added a UITableView to the main view and gave a margin of 10. I added constraints as shown in figure. Changed the Seperator style to None.

Then I added two UITableViewCells.

  1. For Holding the data with custom row height 70.0
  2. Row with same background of parentView with custom row height of 10.0

And implemented the methods like below:

// Row count (Twice as needed, for showing the insect)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 15*2;
}

// Returns cell based on indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    // Decides whether content or inset
    if (indexPath.row%2)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"ReuseInset"];
    }
    else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"ReuseMe"];
        cell.textLabel.text = @"MMP";
    }

    return cell;
}

// Returns custom row height based on indexpath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    if (indexPath.row%2)
    {
        return 10.0;
    }
    return 70.0;
}


来源:https://stackoverflow.com/questions/27554006/how-to-add-margins-on-uitableview-to-inset-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!