【问题标题】:Loading Array into TableView xCode将数组加载到 TableView xCode
【发布时间】:2015-07-27 20:01:20
【问题描述】:

大家好,我是 Swift 和 iOS 编程的新手,我正在尝试创建我的第一个应用程序。我目前正在尝试将数组加载到表视图中,但未加载该数组。我不太确定如何检查,但我一直在关注这个 youtube 视频 https://www.youtube.com/watch?v=pR6dR-vVZeY ,大部分情况下并对其进行调整以适合我的情况。我也没有收到任何错误。这是我的代码。 导入 UIKit

class routineTableViewController: UITableViewController {

    var routines = [Routine]();



    override func viewDidLoad() {
        super.viewDidLoad()

        var routines: [Routine] = [];
        var phatRoutine = Routine(nameOfRoutinex: "PHAT by Layne Norton", typeOfRoutinex: "Mixed");
        routines.append(phatRoutine)
        var smolovRoutine = Routine(nameOfRoutinex: "Smolov", typeOfRoutinex: "Strength");
        routines.append(smolovRoutine)

        for Routine in routines {
            println(Routine.nameOfRoutine);

        }

    }

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

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 0
    }

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

        return routines.count;
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var Cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell;
        Cell.textLabel?.text = routines[indexPath.row].nameOfRoutine;
        return Cell;

    }
}

我正在检查我是否真的使用 for 循环将元素添加到例程中,它说我是。不太确定从这里去哪里。这也是我的故事板,我也不确定是否完全正确。

谢谢你们的帮助。

【问题讨论】:

    标签: ios arrays xcode uitableview


    【解决方案1】:

    UITableView 中的单元格被组织在称为sections 的组中,当您告诉表格它没有任何部分时,它不会显示任何单元格。将节数设置为一:

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }
    

    【讨论】:

    • 试过了,还是没有出现
    • 确保你的视图控制器设置为tableView的delegatedataSource。如果您使用 Interface Builder,您可以在 Connection 检查器中进行检查。
    • 是的,很可能就是这样。
    • 请关闭问题或声明您仍未解决问题,以便社区可以在需要时提供帮助。
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多