【问题标题】:tableView not appearing in ViewController (swift)tableView 没有出现在 ViewController 中(swift)
【发布时间】:2014-11-04 08:19:00
【问题描述】:

在 XCode 6.1 中运行代码时,ViewController 中不会出现任何内容。

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    var messages: [String] = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(self.tableView)

        //http://stackoverflow.com/questions/25413239/custom-uitablecellview-programmatically-using-swift
        //Auto-set the UITableViewCells height (requires iOS8)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44

        // add something to messages
        messages.append("foo")
        messages.append("bar")
    }

    // TABLEVIEWS
    //func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
    //    return 1
    //}

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var count = messages.count ?? 0
        NSLog("message count: \(count)")
        return count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //var cell = tableView.dequeueReusableCellWithIdentifier("MyCell") as MyCell
        var cell = UITableViewCell()
        NSLog("row[\(indexPath.row)]: \(messages[indexPath.row])")
        cell.textLabel.text = messages[indexPath.row]
        return cell
    }

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

【问题讨论】:

    标签: ios uitableview swift uiviewcontroller


    【解决方案1】:

    试试这个代码:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        self.view.addSubview(self.tableView)
    
        //Auto-set the UITableViewCells height (requires iOS8)
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 44
        tableView.frame = CGRectMake(0, 0, 320, 568)
    
        // add something to messages
        messages.append("foo")
        messages.append("bar")
    }
    

    编辑:在所有尺寸的屏幕上试试这个:

    tableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    

    【讨论】:

    • 我已经更新了答案。
    【解决方案2】:

    嗯,为什么要让自己这么难。在情节提要中,您可以将 TableViewController 添加到项目中,也可以将 tableview 添加到您的 viewcontroller 中,它可以开箱即用。控制器类应该继承自 UITableViewController。

    【讨论】:

    • 故事板对你隐藏的太多了。我喜欢能够以编程方式控制我的视图层次结构。
    • 它究竟在应用程序中隐藏了您需要什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多