ios - Swift:TableviewController 中的三个原型(prototype)单元格将无法查看

标签 ios swift uitableview tableview

我想在 tableview Controller 中创建三个单元格,我在 Storyboard 中添加了三个原型(prototype)单元格,它没有崩溃,但它显示的是一个空的 tableview。

 super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    let nib1 = UINib(nibName: "one", bundle: nil)
    tableView.registerNib(nib1, forCellReuseIdentifier: "one")
    let nib2 = UINib(nibName: "two", bundle: nil)
    tableView.registerNib(nib2, forCellReuseIdentifier: "two")
    let nib3 = UINib(nibName: "three", bundle: nil)

    tableView.registerNib(nib3, forCellReuseIdentifier: "three")
}

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 Incomplete implementation, return the number of sections
    return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 1
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

//让 cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) var cell:UITableViewCell!

    switch (indexPath.row){
    case 0 :
        cell = tableView.dequeueReusableCellWithIdentifier("one") as! one
        cell.textLabel?.text = "book1"
        break;
    case 1:
        cell = tableView.dequeueReusableCellWithIdentifier("two") as! two
          cell.textLabel?.text = "book2"
        break;
    case 2 :
        cell = tableView.dequeueReusableCellWithIdentifier("three") as! three
          cell.textLabel?.text = "book3"

    default:
        break;

    }

    // Configure the cell...

    return cell
}

最佳答案

由于您总是希望在表格 View 中显示恰好 3 个单元格,因此您应该使用 UITableView 的“静态单元格”样式。如果您选择适当的 UITableView 并查看属性检查器,则可以在 Storyboard 中设置它。

然后,拖出正确数量的单元格并在 Storyboard 中配置它们。如果您需要以编程方式操作它们或它们的内容,请将每个单元格连接到 IBOutlet

请注意,由于静态 TableView 只允许在 UITableViewController 中使用,因此您可能需要更改包含您在问题中包含的代码的类。

关于ios - Swift:TableviewController 中的三个原型(prototype)单元格将无法查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31364149/

相关文章:

ios - 此日期时间响应的正确格式是什么

ios - 导航返回按钮更新

ios - 在 iOS 中为推送通知发送报告

macos - 禁用 NSTableView 行聚焦环

ios - 如何在 Swift 中从 Google map 的信息窗口跳转到新的 ViewController?

ios - 崩溃的应用程序

iphone - 在哪里构建自定义 UITableViewCell : in layoutSubviews or initWithStyle:reuseIdentifier?

ios - UIImage 到 PHAsset

ios - Xcode (swift) vs Unity for isometric 2d mobile-apps - Performance, Package Size

ios - 直观地改进 iOS 11leadingSwipeActions 的异步操作