简体   繁体   中英

UITableView is not showing up on physical device deployment

I created a UITableView and fed it some data using code.

While running the application on Xcode simulator it works fine but when I deploy it on physical device, the UITableView is not visible.

模拟器截图 在此处输入图片说明 Image - 1 (Simulator) Image - 2 (Device)

Below is my Code:

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
                }
            }
        }
    }
}

Both simulator and iPhone are model 5s, running on iOS 9.2

Seems you must insert these lines in viewDidLoad :

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

Also, in your cellForRowAtIndexPath modify your line:

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

with:

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

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

    self.UITableView.reloadData()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM