gpt4 book ai didi

swift - 我需要在快速动态创建的单元格之间添加一个空格

转载 作者:行者123 更新时间:2023-11-30 11:23:50 26 4
gpt4 key购买 nike

这是我的结构,其中我从 url 下载了 Json

    var bikes = [BikeStats]()

这是我关于 TableView 的声明

    @IBOutlet weak var tableView: UITableView!

这是我用 n 创建 TableView 的代码。行

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return bikes.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
let imgURL = NSURL(string: "my_url/\(bikes[indexPath.row].Immagine)")
if imgURL != nil{
let data = NSData(contentsOf: (imgURL as URL?)!)
cell.imageView?.image = UIImage(data: data! as Data)?.renderResizedImage(newWidth: 70)
}

cell.textLabel?.text = "\(bikes[indexPath.row].StatoBici.uppercased())"
cell.backgroundColor = UIColor.darkGray
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.textLabel?.textColor = UIColor.white
tableView.separatorStyle = UITableViewCellSeparatorStyle.none

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? BikeDetailViewController {
destination.bike = bikes[(tableView.indexPathForSelectedRow?.row)!]
}
}

我需要在动态创建的单元格之间添加一个空格。

最佳答案

要在 UITableViewCell 之间添加间距,您可以这样做。

// Inside UITableViewCell subclass
override func layoutSubviews() {
super.layoutSubviews()

contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(10, 10, 10, 10))
// for only bottom use UIEdgeInsetsMake(0, 0, 10, 0)
}

编辑:- 在您的代码中

class myCustomCell: UITableViewCell {

override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, UIEdgeInsetsMake(0, 0, 10, 0))
}
}

然后在tableView中:cellForRowAt indexPath像这样使用单元格

let cell = myCustomCell(style: .subtitle, reuseIdentifier: nil)

关于swift - 我需要在快速动态创建的单元格之间添加一个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50973610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com