首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableViewCell中的UILabel返回nil

UITableViewCell中的UILabel返回nil
EN

Stack Overflow用户
提问于 2019-08-29 04:21:35
回答 1查看 43关注 0票数 0

我想在UIViewController中设置一个UITableView。UITableView有UITableViewCells,它有一个UILabel。

但是,UITableView.UILabels返回nil。

请看下面的代码和图片,并分享你的想法。

提前谢谢你。

我正在和Swift5一起开发这个。

代码语言:javascript
复制
import UIKit

class condimentTableCell: UITableViewCell {
    @IBOutlet weak var condimentName: UILabel!
}

class SandwichViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var condimentTable: UITableView!
    @IBOutlet weak var breadSelector: UIPickerView!

    fileprivate let items:[product] = products().getData()
    fileprivate let cart:cart = Registry.instance.liveCart

    var itemNo: Int = 0
    let condiments = ["Lettuce", "Mayo", "Mustard", "Purple Onions", "Tomato"]
    let breads = ["Rye", "Sourdough", "Squaw", "Wheat", "White"]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
        self.breadSelector.dataSource = self
        self.breadSelector.delegate = self
        self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")
        self.condimentTable.delegate = self
        self.condimentTable.dataSource = self
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return condiments.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:condimentTableCell = tableView.dequeueReusableCell(withIdentifier: "condimentCell", for: indexPath) as! condimentTableCell
        if (cell.condimentName == nil) {
            print("nil")
        } else {
            cell.condimentName.text = condiments[indexPath.row]
        }
        print("----------------------------------------------")
        return cell
    }


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return breads.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return breads[row]
    }
}

simulator

print result

Storyboard-1

Storyboard-2

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-29 05:29:28

删除行

代码语言:javascript
复制
self.condimentTable.register(condimentTableCell.self, forCellReuseIdentifier: "condimentCell")

您正在断开与单元格的情节提要连接。

有关详细说明,请参阅https://stackoverflow.com/a/44107114/669586

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57699555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档