簡體   English   中英

Swift中的自定義表格視圖單元格,沒有Storyboard

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

我是學習iOS開發和Swift語言的新手,並且一直試圖弄清楚如何在不使用Storyboards / Interface Builder的情況下創建自定義UITableViewCell 我希望能夠在Swift代碼中完成所有工作。 到目前為止,我真的只能找到那些使用Interface Builder的人。

我希望能夠做的是創建一個可以在任何表視圖中實例化的可重用單元。 根據我的理解,我應該能夠創建一個帶有子視圖的自定義單元格,可以通過表格視圖的傳入數據設置數據。 對?

現在我有一個帶有嵌入式子類UITableViewControllerUINavigationController 在其他一些教程之后,我還學習了如何創建一個Struct並為表視圖單元格准備測試數據。 但這就是我能夠得到的。

// 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?
}

我知道Swift仍然很新,這可能就是為什么我找不到太多資源來討論這個話題,所以如果有人有任何指示我會非常感謝你的幫助!

您可以在沒有storyboard的情況下使用以下代碼來自定義單元格。只需注冊您的class並將其與tableViewController

//Cell.Swift

import Foundation
import UIKit

class Cell : UITableViewCell{

    //Do whatever you want as exta customization
}

你的控制器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