代码之家  ›  专栏  ›  技术社区  ›  Rohit Singh Varun

TableViewCell在Swift中无法获取数据

  •  0
  • Rohit Singh Varun  · 技术社区  · 4 年前

    我在TableViewController中使用自定义TableViewCell。 即使数据正在填充,但我的TableViewCell也不会显示它。

    这是我的自定义TableViewCell代码

    class PlaceItemCellCustom: UITableViewCell {
    
       @IBOutlet weak var placeFace: UILabel?
       @IBOutlet weak var placeName: UILabel?
    
       override func awakeFromNib() {
          super.awakeFromNib()
       } 
    
    }
    

    这是我的TableViewOntoller

    class PlaceListViewController: UITableViewController {
    
       override func viewDidLoad() {
          super.viewDidLoad()
          loadListFromSource()
       }
      
       // Other code
    
       override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
       }
    
       override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return places.count
       }
    
       override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
           return 100
       }
    
       override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
           let place = places[indexPath.row]
           // I am getting the data 
           print(place.placeName?.description ?? " " )
        
           let customCell = tableView.dequeueReusableCell(withIdentifier: "PlaceListIdentifier", for: indexPath) as! PlaceItemCellCustom
        
           customCell.placeName?.text = place.placeName
       
           return customCell
       }
    
    }
    

    我在这里错过了什么?我是IOS应用程序开发的新手。 发现类似 questions 但无济于事

    编辑:默认的TableViewCell运行良好。

    但是当我将其更改为自定义TableViewCell时,它不起作用。我也在故事板中设置了类名。

    这是我的故事板

    enter image description here

    这是输出

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  1
  •   Amit    4 年前

    据我所知,你一定忘了连接 IBOutlet PlaceItemCellCustom中的地名:

    enter image description here

    在PlaceItemCellCustom中,将placeName保留为:

    @IBOutlet weak var placeName: UILabel!
    

    和in cellForRowAt :

    customCell.placeName.text = place.placeName
    

    这样,如果你忘记连接segue,Xcode将抛出错误如下:

    线程1:严重错误:隐式展开Optional值时意外发现nil

    您可以在此处阅读更多信息:

        2
  •  0
  •   B.Dhruvin    4 年前

    您需要使用此方法来获取数据。

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