繁体   English   中英

Swift中具有数组的表视图

[英]Table view with array in Swift

我试图在我的应用程序中完成对视图控制器的简单添加,但是事实证明这是一个挑战。 我试图在本质上向此视图控制器添加一个待办事项列表。 我有一个数组“列表”,当我运行该应用程序时,其项目应显示,但出现空白。 我已经检查了常见错误,例如未将表视图连接到视图控制器,对表视图没有约束等等。 我看不到我在做什么错,但是希望另一双眼睛可以将我指向正确的方向。 代码如下:

class ThirteenthViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var list = ["hero", "The Boys of Brighton Beach", "This Time is Different", "BS Book"]

@IBOutlet weak var collectionTableView: UITableView!

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return (list.count)

}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")

    cell.textLabel?.text = list[indexPath.row]

    return(cell)

}

func tableView(  tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{

    if editingStyle == UITableViewCellEditingStyle.delete
    {

        self.list.remove(at: indexPath.row)
        collectionTableView.reloadData()

    }

}


@IBAction func dismissCollection(_ sender: Any) {

    dismiss(animated: true, completion: nil)

}


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

您必须在viewDidLoad函数上添加这两行

self.collectionTableView.dataSource = self
self.collectionTableView.delegate = self

长解释,您必须告诉控制器从何处获取数据,因此使用该代码,您将说dataSource来自此控制器以及委托。

希望能帮助到你。

您需要在情节提要中将dataSource添加到表视图中。 此链接可以帮助你解答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM