swift - 如何在间距表视图单元格中正确显示数据
问题描述
只有数组中的第一个元素显示在我的间距单元格中。
我在没有间距的简单单元格中显示数据没有问题,但是当我需要间距单元格并更改我的代码时,只显示第一项
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
解决方案
代替
cell.namePlace.text = test[indexPath.row]
和
cell.namePlace.text = test[indexPath.section]
间距是因为您显示部分,因此您需要访问数组而section
不是row
推荐阅读
- json - 来自 applicationinsights 跟踪日志的 kusto 查询返回空字段
- wordpress - 错误:找不到版本 11 (PostgreSQL 11) 的 pg_ctl 可执行文件 + 让我们加密
- sqlite - SQLite:基于共享指标添加整数表
- c - 双链表升序排序
- php - 使用自定义缓存后端时,通过 cli 执行的 TYPO3 调度程序任务不会清除页面缓存
- python - 在熊猫中读取文本文件的问题
- javascript - Laravel Ajax - 未捕获的 ReferenceError:KWS1389776 未在 HTMLButtonElement.onclick 中定义
- r - 不同数据集中同一个人的两个不同 id 值
- java - 关联来自多个源的对象
- azure-media-services - 在直播视频流期间播放两个音频源