首页 > 解决方案 > 如何在间距表视图单元格中正确显示数据

问题描述

只有数组中的第一个元素显示在我的间距单元格中。

我在没有间距的简单单元格中显示数据没有问题,但是当我需要间距单元格并更改我的代码时,只显示第一项

let test = ["1","2","3"]
func numberOfSections(in tableView: UITableView) -> Int {
    return test.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection 
    section: Int) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    let cellSpacingHeight: CGFloat = 10
    return cellSpacingHeight
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView()
    headerView.backgroundColor = UIColor.clear
    return headerView
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "SellerPlacesCell", for: indexPath) as! SellerPlacesCell
    cell.backgroundColor = UIColor.white.withAlphaComponent(0.5)
    cell.namePlace.text = test[indexPath.row]
    return cell
}

在此处输入图像描述 如何解决?

如果在

func tableView(_ tableView: UITableView, numberOfRowsInSection 
    section: Int) -> Int {
    return 1
}
return 1 

我替换return 1test.count这个: 在此处输入图像描述

标签: swiftuitableview

解决方案


代替

cell.namePlace.text = test[indexPath.row]

cell.namePlace.text = test[indexPath.section]

间距是因为您显示部分,因此您需要访问数组而section不是row


推荐阅读