【问题标题】:custom UITableViewCell - not showing自定义 UITableViewCell - 不显示
【发布时间】:2021-10-14 22:17:48
【问题描述】:

我是 Swift 新手。 我尝试制作一个自定义的 TableView 并在 StoryBoard 中添加了一个原型单元

我在单元格的检查器中添加了自定义类

这里是代码:

class SearchTableViewCell: UITableViewCell {

    @IBOutlet var routeDescriptionLbl: UILabel!
    @IBOutlet var ivRoute: UIImageView!
    @IBOutlet var distanceLbl: UILabel!
    @IBOutlet var routeNumberLbl: UILabel!


    override func awakeFromNib() {
     //still empty yet
        super.awakeFromNib()

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

现在我做了一个标识符:

这是整个视图(部分)的视图控制器:

class SearchViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    var transportData : [RouteModel] = []

    override func viewDidLoad() {
                super.viewDidLoad()
        fillMockup()
        self.lvMain.reloadData() //in case it's needed after mockup is filled

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewDidAppear(animated: Bool) {

        NSLog("viewDidAppear")
    }

    func fillMockup(){
//just some mockup data to test if it's working
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
        transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
        transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
        transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))
    }

    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        NSLog("transportData.count = \(transportData.count)")
        return transportData.count
    }

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as? SearchTableViewCell
        if let cell = cell {
            cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
            cell.routeDescriptionLbl.text = transportData[indexPath.row].description
            cell.distanceLbl.text = transportData[indexPath.row].dist

            return cell
        }

        return UITableViewCell()

    }

    func tableView(tableView: UITableView,
                   didSelectRowAtIndexPath indexPath: NSIndexPath){
        NSLog("You selected cell #\(indexPath.row)!")
        let position = indexPath.row;
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
    }
}

现在,没有调用表视图中的任何日志。当然,TableView 没有显示

有什么问题?

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    喜欢

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell
    
         cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
            cell.routeDescriptionLbl.text = transportData[indexPath.row].description
            cell.distanceLbl.text = transportData[indexPath.row].dist
    
        return cell
    }
    

    更新答案

    override func viewDidLoad() {
                    super.viewDidLoad()
              self.lvMain.delegate = self // invoke data source and delegate on current class
             self.lvMain.dataSource = self
            fillMockup()
    
    
    
        }
    
      func fillMockup(){
    //just some mockup data to test if it's working
            transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "44", description: "Ярославль - Сан Тропе",dist: "200 m"))
            transportData.append(RouteModel(type: RouteModel.TYPE_TRAM,routeNumber: "11", description: "Ярославль - Сан Тропе",dist: "400 m"))
            transportData.append(RouteModel(type: RouteModel.TYPE_R_BUS,routeNumber: "1", description: "Ярославль Главный - Жопа Мира",dist: "1.4 км"))
            transportData.append(RouteModel(type: RouteModel.TYPE_TROLLEY,routeNumber: "24", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
            transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
            transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "43а", description: "Ярославль - Малые Гребеня",dist: "1.4 км"))
            transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "85б", description: "Ярославль - Брагино",dist: "1.4 км"))
            transportData.append(RouteModel(type: RouteModel.TYPE_BUS,routeNumber: "38", description: "Ярославль - Брагино",dist: "2.4 км"))
    
          self.lvMain.reloadData() //reload the table when does loaded perfectly 
        }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell: SearchTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier") as! SearchTableViewCell
    
         cell.routeNumberLbl.text = transportData[indexPath.row].routeNumber
            cell.routeDescriptionLbl.text = transportData[indexPath.row].description
            cell.distanceLbl.text = transportData[indexPath.row].dist
    
        return cell
    }
    

    更多示例见this

    【讨论】:

      【解决方案2】:

      viewDidLoad()函数内添加两行

      self.yourtableviewname.delegate = self
      self.yourtableviewname.dataSource = self
      

      【讨论】:

        【解决方案3】:

        发生这种情况是因为您的单元格总是创建一个新实例,因为它没有进入 if 部分。因此,请检查您的单元格是否为 nil,然后像这样创建您的单元格的实例..

            func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        
            var cell: MyCell! = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier")
        
            if cell == nil {
                        tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "reuseIdentifier")
                       cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? MyCell
                    }
            return cell
          }
        }
        

        这里的 MyCell 是自定义单元格。

        【讨论】:

          【解决方案4】:

          如果您正在使用情节提要,请从 viewdidload 函数中删除此行

          self.tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
          

          它对我有用。

          【讨论】:

            【解决方案5】:

            您可以通过以下两个步骤检查您是否忘记执行任何操作

            1)交叉检查是否连接了Datasource、Delegate和IBoutlet For tableview?

            2)检查你在Storyboard中设置的标识符是否与reuseIdentifier相同?

            【讨论】:

              【解决方案6】:

              您需要在 viewDidLoad 或 Stroyboard 中自定义 tableView Delegate 和 Datasource。

              override func viewDidLoad() {
                super.viewDidLoad()
              
                tableViewName.delegate = self
                tableViewName.dataSource = self
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2014-09-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-10-15
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多