swift - 我需要在快速动态创建的单元格之间添加一个空格
问题描述
这是我的结构,里面我从 url 下载了一个 Json
var bikes = [BikeStats]()
这是我关于表格视图的声明
@IBOutlet weak var tableView: UITableView!
这是我用 n 创建表格视图的代码。行
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)!]
}
}
我需要在动态创建的单元格之间添加一个空格。
解决方案
如果您想要单元格之间的空白行,您需要增加行数并将交替单元格配置为空白而不是填充内容。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return bikes.count * 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
// affect only the even numbered cells (0, 2, 4, 6, 8 etc.)
if indexPath.row % 2 == 0 {
cell.imageView?.image = nil
cell.textLabel?.text = ""
// set the colors to whatever you want
cell.backgroundColor = UIColor.darkGray
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.textLabel?.textColor = UIColor.white
return cell
}
let dataIndex = indexPath.row / 2 // we need to do this because we're doubling the number of rows
let imgURL = NSURL(string: "my_url/\(bikes[dataIndex].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[dataIndex].StatoBici.uppercased())"
cell.backgroundColor = UIColor.darkGray
cell.layer.borderColor = UIColor.darkGray.cgColor
cell.textLabel?.textColor = UIColor.white
tableView.separatorStyle = UITableViewCellSeparatorStyle.none
return cell
}
推荐阅读
- angular - Typescript 接口类型变量,在数组中实现
- javascript - 在 JSX 映射函数中仅重新渲染特定的子组件
- javascript - 如何在反应打字稿中将路由链接添加到材料 ui 选项卡
- python - 将字典写入行中的 txt 格式 - Pandas
- rust - 如何安装 +nightly (无论这意味着什么),这样我就可以运行 cargo expand
- java - firebase 数据库数据中的混合数据
- reactjs - 更好地使用 useEffect 钩子而不需要使用回调
- asp.net - 即使 web.config 设置为允许,ASP.NET 也会为文件返回 401 Unauthorized?
- python - 基于使用预训练嵌入的词集的词义消歧
- heroku - Heroku 应用程序崩溃