为了账号安全,请及时绑定邮箱和手机立即绑定

如何在UITableViewCell之间添加间距

如何在UITableViewCell之间添加间距

陪伴而非守候 2019-08-15 16:32:04
如何在UITableViewCell之间添加间距有没有办法增加间距UITableViewCell?我创建了一个表,每个单元格只包含一个图像。图像被分配给单元格,如下所示:cell.imageView.image = [myImages objectAtIndex:indexPath.row];但这会使图像放大并适合整个单元格,图像之间没有间距。或者以这种方式说,图像的高度例如是50,并且我想在图像之间添加20个间隔。有没有办法实现这个目标?
查看完整描述

3 回答

?
四季花海

TA贡献1811条经验 获得超5个赞

Swift版本

针对Swift 3进行了更新

为了未来的观众,这个答案比原始问题更为笼统。它是Swift的基本UITableView示例的补充示例。

概观

基本思想是为每个数组项创建一个新的部分(而不是一个新行)。然后可以使用截面标题高度来隔开这些截面。

怎么做

  • 按照Swift的UITableView示例中的描述设置项目。(也就是说,添加一个UITableView并将tableView插座连接到View Controller)。

  • 在Interface Builder中,将主视图背景颜色更改为浅蓝色,将UITableView背景颜色更改为清除。

  • 用以下内容替换ViewController.swift代码。

ViewController.swift

import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    // These strings will be the data for the table view cells
    let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]

    let cellReuseIdentifier = "cell"
    let cellSpacingHeight: CGFloat = 5

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // These tasks can also be done in IB if you prefer.
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self    }

    // MARK: - Table View delegate methods

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.animals.count    }

    // There is just one row in every section
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    // Set the spacing between sections
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return cellSpacingHeight    }

    // Make the background color show through
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.clear        return headerView    }

    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

        // note that indexPath.section is used rather than indexPath.row
        cell.textLabel?.text = self.animals[indexPath.section]

        // add border and color
        cell.backgroundColor = UIColor.white
        cell.layer.borderColor = UIColor.black.cgColor
        cell.layer.borderWidth = 1
        cell.layer.cornerRadius = 8
        cell.clipsToBounds = true

        return cell    }

    // method to run when table view cell is tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // note that indexPath.section is used rather than indexPath.row
        print("You tapped cell number \(indexPath.section).")
    }}

请注意,indexPath.section使用而不是indexPath.row为了获得数组元素和分接位置的正确值。

你是如何在左右两侧获得额外的填充/空间的?

我得到它的方式与为任何视图添加间距的方式相同。我使用了自动布局约束。只需使用Interface Builder中的pin工具为前导和尾随约束添加间距。


查看完整回答
反对 回复 2019-08-15
?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

我在单元格之间添加间距的方法是使numberOfSections =“你的数组计数”并使每个部分只包含一行。然后定义headerView及其高度。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return yourArry.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;}-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return cellSpacingHeight;}-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v = [UIView new];
    [v setBackgroundColor:[UIColor clearColor]];
    return v;}


查看完整回答
反对 回复 2019-08-15
?
ITMISS

TA贡献1871条经验 获得超8个赞

我使用Swift的简单解决方案:

// Inside UITableViewCell subclassoverride func layoutSubviews() {
    super.layoutSubviews()

    contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))}


查看完整回答
反对 回复 2019-08-15
  • 3 回答
  • 0 关注
  • 2056 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信