这是我的结构,其中我从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)!]
}
}
我需要在动态创建的单元格之间添加一个空格。
答案 0 :(得分:0)
如果要在单元格之间保留空白行,则需要增加行数并将交替的单元格配置为空白而不是填充内容。
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
}
答案 1 :(得分:0)
要在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)
答案 2 :(得分:0)
由于您未在tableView中使用节,我建议您使用此方法
extension SomeViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return bikes.count
}
}
extension SomeViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) ->
UIView? {
return UIView(frame: CGRect(x: 0, y: 0, width: tableview.frame.width, height: 10))
}
}