【发布时间】:2017-08-19 01:06:08
【问题描述】:
我正在用列表中的项目填充表格视图,当我向表格视图添加多个项目时,除了第一个单元格之外的所有单元格都是空白的。当我删除屏幕底部最低的单元格时,它会将来自该单元格的数据放入下一个单元格中。我不确定我做错了什么,但如果有人能够提供帮助,这是我的代码。
import UIKit
// Initialize the list that populates the each cell in the table view
var list = [AnyObject]()
let cell = UITableViewCell(style: UITableViewCellStyle.default,
reuseIdentifier: "cell")
class FirstViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
// Declares how many rows the table view will have
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (list.count)
}
// Populates each table view cell with items from each index in the list
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
// cell.accessoryType = .checkmark
// cell.textLabel?.textAlignment = .natural
// The text of the cell = the data of index that matches the index of the cell
cell.textLabel?.text = list[indexPath.row] as? String
// Sets the background color of the cell
// cell.backgroundColor = UIColor.clear
return cell
}
// Allows user to remove item from table view by swiping left
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
// if the user swiped left to delete an item from the table view
if editingStyle == UITableViewCellEditingStyle.delete
{
// remove the deleted item from the list and reload the data
list.remove(at: indexPath.row)
myTableView.reloadData()
}
}
【问题讨论】:
-
您需要从一千个表格视图教程中找到任何一个,并从
cellForRowAt方法中查看提供单元格的正确方法。 -
@maddy 这就是我想要弄清楚的。我看过几个教程。如果您能提供一些关于我的 cellForRowAt 方法做错了什么的见解,那将很有帮助。
标签: ios swift uitableview