【问题标题】:How can I change the space between tableview cells ios swift如何更改tableview单元格之间的空间ios swift
【发布时间】:2015-11-26 09:54:44
【问题描述】:

如何更改 tableviewcontroller 中单元格之间的间距?。我有一个使用 Swift 的应用程序。我添加了一个工作正常的 tableviewcontroller。现在我的问题是,如果我尝试更改它不起作用的单元格之间的空间。我试过下面的代码。

 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10; // space b/w cells
    }

请帮我找出问题所在?

编辑 1:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        let a = 1;
        return a;
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let a = itemArray.count;
        return a;
    }

【问题讨论】:

  • 此代码用于部分之间的空间。如果你想使用它,你必须改变numberOfRows...和numberOfSections...的逻辑......
  • 这篇文章与post重复
  • 你搜索过这个网站吗?见:stackoverflow.com/questions/7189523/…
  • @Drizztneko 我已经设置了行数。我会更新我的问题。请告诉我我需要如何更改逻辑
  • 您可以从 xib 更改单元格大小

标签: ios uitableview swift2


【解决方案1】:

要使用方法,你必须改变逻辑

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    let a = itemArray.count;
    return a;
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let a = 1;
    return a;
}

并且还必须更改 rowAtIndexPath...,使用 indexPath.section 而不是 indexPath.row,例如

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell")

    cell.titleLabel.text = itemArray[indexPath.section] // if itemArray is [String]

    return cell
}

【讨论】:

  • 我不明白。你想说什么?
  • 查看我的编辑,这就是您访问阵列的方式
  • 我正在使用这个函数。所以你的意思是我只想在你的答案中删除前两个覆盖函数并从下一个权利改变?
  • 不,您必须使用所有功能。表格视图需要所有这些,第一个用于了解节数,第二个用于了解每个节中的行数,最后一个用于填充每一行。您还需要在问题中编写的函数来建立它们之间的空间。
【解决方案2】:

使用 Swift 2,您可以通过这种方式在 UITableViewCells 之间设置间距:

// In this case I returning 140.0. You can change this value depending of your cell
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 140.0
}

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.contentView.backgroundColor = UIColor.clearColor()

    let whiteRoundedView : UIView = UIView(frame: CGRectMake(0, 10, self.view.frame.size.width, 120))

    whiteRoundedView.layer.backgroundColor = CGColorCreate(CGColorSpaceCreateDeviceRGB(), [1.0, 1.0, 1.0, 1.0])
    whiteRoundedView.layer.masksToBounds = false
    whiteRoundedView.layer.shadowOffset = CGSizeMake(-1, 1)

    cell.contentView.addSubview(whiteRoundedView)
    cell.contentView.sendSubviewToBack(whiteRoundedView)
}

【讨论】:

  • 看,您也可以创建自己的自定义单元格。稍后,使高度等于 120,向其中添加元素并添加边距 20,从底部到最后一个元素。它看起来就像你想要的那样
【解决方案3】:

您可以使用布局边距功能。这仅适用于 iOS8。子类化 UITableViewCell 和

- (UIEdgeInsets)layoutMargins {
    return UIEdgeInsetsMake(10, 0, 10, 0);
}

【讨论】:

    【解决方案4】:

    嗨,我发现最简单的方法就是改变它 来自:

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            let a = 1
            return a
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let a = itemArray.count
            return a
        }

    收件人:

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
            let a = itemArray.count
            return a
        }
    
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let a = 1
            return a
        }

    并添加:

    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return heightOfSpaceBetweenCells
        }
        
        override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            let footerV = UIView()
                footerV.backgroundColor = UIColor.clearColor()
    
                return footerV
        }

    【讨论】:

      猜你喜欢
      • 2020-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多