【问题标题】:UITableView is not showing up on physical device deploymentUITableView 未显示在物理设备部署中
【发布时间】:2016-04-10 12:31:54
【问题描述】:

我创建了一个 UITableView 并使用代码为其提供了一些数据。

在 Xcode 模拟器上运行应用程序时,它工作正常,但是当我将它部署到物理设备上时,UITableView 不可见。

图片 - 1(模拟器) 图片 - 2(设备)

以下是我的代码:

import UIKit

class OTCMedicines: UIViewController, UITableViewDelegate, UITableViewDataSource{


    @IBOutlet weak var tableView: UITableView!
    struct Med {

        var name:String
        var detail:String
        var imageName:String
        var image: UIImage {
            get {
                return UIImage(named: imageName)!
            }
        }

    }
    var data:[Med]=[Med]()

    override func viewDidLoad() {
        super.viewDidLoad()
        print("OTC Loaded")
        data.append(Med(name:"Nimprex P",detail:"Fever and Painkiller",imageName:"db"))
        data.append(Med(name:"Cozi Plus",detail:"Cold and Fever",imageName:"db"))
        data.append(Med(name:"Combiflam",detail:"Headach and Painkiller",imageName:"db"))
        data.append(Med(name:"Flexon",detail:"Muscle Painkiller",imageName:"db"))
        data.append(Med(name:"Avil",detail:"Antibiotic",imageName:"db"))
        data.append(Med(name:"Cetirizine",detail:"Antibiotic and Allergy",imageName:"db"))
        data.append(Med(name:"LIV 52",detail:"Lever Problems",imageName:"db"))
        data.append(Med(name:"Perinorm",detail:"Stomach-ach and Puke",imageName:"db"))
        data.append(Med(name:"Edicone Plus",detail:"Fever and Cold",imageName:"db"))
        data.append(Med(name:"L-Hist Mont",detail:"Peanut Allergies",imageName:"db"))
        tableView.delegate = self
        tableView.dataSource = self
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MedicineCell") as! OTCMedCell!
        let medView = data[indexPath.row]
        print("OTC Table Cell Loaded")
        cell.medName.text = medView.name
        cell.medImage.image = medView.image
        cell.medDetail.text = medView.detail

        return cell
    }
    // MARK:  UITableViewDelegate Methods
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "OTCMedPush" {
            if let destination = segue.destinationViewController as? OTCMedicineDetail {
                if let medIndex = tableView.indexPathForSelectedRow?.row {
                    destination.medicineValue = data[medIndex].name
                }
            }
        }
    }
}

模拟器和 iPhone 都是 5s 型号,在 iOS 9.2 上运行

【问题讨论】:

    标签: ios iphone swift uitableview


    【解决方案1】:

    我相信您在 viewDidLoad 中添加了所有元素后需要重新加载表格。

        self.UITableView.reloadData()
    

    【讨论】:

      【解决方案2】:

      看来您必须在 viewDidLoad 中插入这些行:

      self.tableView.registerNib(UINib(nibName: "OTCMedCell", bundle:nil), forCellReuseIdentifier: "MedicineCell")
      self.tableView.reloadData()
      

      另外,在您的 cellForRowAtIndexPath 中修改您的行:

      let cell = tableView.dequeueReusableCellWithIdentifier("MedicineCell") as! OTCMedCell!
      

      与:

      let cellIdentifier : String! = "MedicineCell"
      var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? OTCMedCell!
      if cell == nil {
         cell = OTCMedCell(style: UITableViewCellStyle.Default, reuseIdentifier: (cellIdentifier))
      }
      

      【讨论】:

      • 'let' 不允许我们修改变量。您正在条件范围内对其进行修改。
      猜你喜欢
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 2018-09-27
      • 1970-01-01
      相关资源
      最近更新 更多