简体   繁体   English

Swift中的自定义表格视图单元格,没有Storyboard

[英]Custom table view cell in Swift, without Storyboard

I'm new to learning iOS development and the Swift language, and have been trying to figure out how to create a custom UITableViewCell without the use of Storyboards/Interface Builder. 我是学习iOS开发和Swift语言的新手,并且一直试图弄清楚如何在不使用Storyboards / Interface Builder的情况下创建自定义UITableViewCell I'd like to be able to accomplish everything in Swift code. 我希望能够在Swift代码中完成所有工作。 So far, I've only really been able to find ones that make use of the Interface Builder. 到目前为止,我真的只能找到那些使用Interface Builder的人。

What I would like to be able to do is create a re-usable cell that can be instantiated in any table view. 我希望能够做的是创建一个可以在任何表视图中实例化的可重用单元。 From what I understand I should be able to create a custom cell with sub-views, whos data can be set by the table view's incoming data. 根据我的理解,我应该能够创建一个带有子视图的自定义单元格,可以通过表格视图的传入数据设置数据。 Right? 对?

Right now I have a UINavigationController with an embedded, sub-classed UITableViewController . 现在我有一个带有嵌入式子类UITableViewControllerUINavigationController Following some other tutorials I've also learned how to create a Struct and prepare test data for the table view cell. 在其他一些教程之后,我还学习了如何创建一个Struct并为表视图单元格准备测试数据。 But that's about as far as I've been able to get. 但这就是我能够得到的。

// My Table View Controller
class InventoryListViewController: MainTableViewController {

    let viewTitle = "Inventory"
    var inventoryItems = [InventoryItem]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = viewTitle.uppercaseString

        self.inventoryItems = [
            InventoryItem(name: "White Bread", expiry: "2014-09-12"),
            InventoryItem(name: "Mushrooms", expiry: "2014-09-15"),
            InventoryItem(name: "Watermelon", expiry: nil),
            InventoryItem(name: "Leftover Thai", expiry: "2014-09-15"),
            InventoryItem(name: "Cheddar Cheese", expiry: "2014-09-12"),
            InventoryItem(name: "Chicken Breasts", expiry: "2014-09-10"),
            InventoryItem(name: "Paprika", expiry: nil),
            InventoryItem(name: "Sour Cream", expiry: nil)
        ]

        // Right now this will fail without tableView:cellForRowAtIndexPath
        self.tableView.reloadData()
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.inventoryItems.count
    }

}

// Sub-classed UITableViewController
class MainTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.separatorColor = UIColor.clearColor()
        self.tableView.contentInset = UIEdgeInsetsZero
    }

}

// Data struct for cells
struct InventoryItem {
    let name: String
    let expiry: String?
}

I understand Swift is still very new and that's probably why I can't find too many resources for this topic, so if anyone has any pointers I would be very appreciative to the help! 我知道Swift仍然很新,这可能就是为什么我找不到太多资源来讨论这个话题,所以如果有人有任何指示我会非常感谢你的帮助!

You can use below code for custom cells without storyboard.Just register your class and use it with tableViewController 您可以在没有storyboard的情况下使用以下代码来自定义单元格。只需注册您的class并将其与tableViewController

//Cell.Swift

import Foundation
import UIKit

class Cell : UITableViewCell{

    //Do whatever you want as exta customization
}

Your controller viewDidLoad 你的控制器viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    // register your class with cell identifier
    self.tableView.registerClass(Cell.self as AnyClass, forCellReuseIdentifier: "Cell")
}


//cellForRowAtIndexPath

  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:Cell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? Cell
        if cell == nil {

            cell = Cell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }
        //configure your cell custom code
         cell.textLabel?.text = @"Your Label Text"
        return cell!
    }

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

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