如何在UITableView上添加边距以插入内容 [英] How to add Margins on UITableView to inset content

查看:108
本文介绍了如何在UITableView上添加边距以插入内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,我想包含边距,以便表格的内容在左右两侧以及单元格之间都有一定的呼吸空间.

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.

推荐答案

我设法制作了一个表格视图,例如:

I managed to do a table view like:

我的分镜脚本设计就像:

My Storyboard design is like:

我所做的是在主视图中添加了UITableView,并给了10的余量.我添加了如图所示的约束.将Seperator style更改为None.

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.

然后我添加了两个UITableViewCell.

  1. 用于保存自定义行高为70.0的数据
  2. 具有与parentView相同背景的行,自定义行高为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;
}

这篇关于如何在UITableView上添加边距以插入内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆