簡體   English   中英

在Swift中連接UITableViewCell標簽

[英]Connecting a UITableViewCell Label in Swift

我有一個TableView單元格,里面有一個ImageViewLabel 但是當我使用以下方法連接到它們時:

 @IBOutlet weak var menuListLabel: UILabel!
 @IBOutlet weak var menuListImage: UIImageView!

在此處輸入圖片說明

非法配置:

從ViewController到UIImageView的menuListImage插座無效。 插座不能連接到重復內容。

您需要創建一個繼承自UITableViewCell的自定義類,並在那里配置出口。

class MyCustomTableViewCell: UITableViewCell {
   @IBOutlet weak var menuListLabel: UILabel!
   @IBOutlet weak var menuListImage: UIImageView!
}

接下來,您需要在情節提要中配置單元格。 選擇您的單元格。 打開身份檢查器並將“自定義類”設置為“ MyCustomTableViewCell”。

然后,在仍選擇單元格的情況下,轉到“屬性”檢查器,然后將“重用標識符”設置為“ MyCustomTableViewCell”。 (此標識符可以是您想要的任何名稱,只需在調用'dequeueReusableCellWithIdentifier'時使用此確切值即可。我喜歡使用單元格的類名作為標識符,這樣很容易記住。)

在表視圖控制器中,實現必要的方法以使用自定義單元格構建表。

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1   // however many sections you need
}

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

    return 1   // however many rows you need
}

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

    // get an instance of your cell
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath: indexPath) as MyCustomTableViewCell

    // populate the data in your cell as desired
    cell.menuListLabel.text = "some text"
    cell.menuListImage.image = UIImage(named: "some image")

    return cell
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM