Swift 3自定义TableViewCell错误

时间:2016-12-02 16:49:09

标签: swift tableview

我无法与" TableView"进行通信。我该如何解决这个问题呢?

Error Image

Code Image

var arrayOfData = [cellData]()
var fullname1 : String!
var code1 : String!
var name1 : String!
var buy1 : Double!
var sell1 : Double!
var change_rate1 : Double!
var update_date1 : Double!

var arrayFullName : [String] = ["asdas","asds","asds","sadas","asdasd","asdas"]


@IBOutlet weak var tblDoviz: UITableView!
@IBOutlet weak var openBarButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()
    arrayOfData = [cellData(fullName: "Amerikan Doları", name: "", code: "USD", updateDate: 2, changeRate: 2, buy: 3.44, sell: 3.47)]
    openBarButton.target = self.revealViewController()
    openBarButton.action = Selector("revealToggle:")

    tblDoviz.delegate = self
    tblDoviz.dataSource = self

    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    /* Doviz Sitesinden Bilgiler Çekiliyor */

    let url = NSURL(string: "http://www.doviz.com/api/v1/currencies/all/latest")
    let task = URLSession.shared.dataTask(with: url as! URL){(data,response,error) ->Void in


    if error != nil
    {
    }
        else
    {
        if let urlContent = data
        {
            do
            {

                let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                if let currencyRate  = jsonResult as? NSArray
                {

                for i in 0..<currencyRate.count
                {
                    if let name = (currencyRate[i] as? NSDictionary)?["name"] as? String
                    {
                      self.name1 = name
                    }
                    if let fullname = (currencyRate[i] as? NSDictionary)?["full_name"] as? String
                    {
                      self.fullname1 = fullname


                    }
                    if let change_rate = (currencyRate[i] as? NSDictionary)?["change_rate"] as? Double
                    {
                       self.change_rate1 = change_rate


                    }
                    if let code = (currencyRate[i] as? NSDictionary)?["code"] as? String
                    {
                        self.code1 = code


                    }
                    if let update_date = (currencyRate[i] as? NSDictionary)?["update_date"] as? Double
                    {

                        self.update_date1 = update_date

                    }
                    if let buying = (currencyRate[i] as? NSDictionary)?["buying"] as? Double
                    {

                           self.buy1 = buying


                    }
                    if let selling = (currencyRate[i] as? NSDictionary)?["selling"] as? Double
                    {

                            self.sell1 = selling

                    }

                    //Array'a Yükleme Yapılıyor
                    //cellData.init(fullName: self.fullname1, name: self.name1, code: self.code1, updateDate: self.update_date1, changeRate: self.change_rate1, buy: self.buy1, sell: self.sell1)
                    //self.arrayOfData = [cellData(fullName: self.fullname1, name: self.name1, code : self.code1, updateDate: self.update_date1, changeRate: self.change_rate1, buy: self.buy1, sell: self.sell1)]
                    //print(self.arrayOfData)
                    //print(cellData())

                }
                    print(self.arrayFullName.count)
                }
            }
            catch
            {

            }

        }

        }
    }
    task.resume()

    //--------------------------------------------------
}

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

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellDoviz",for:indexPath) as! DovizCell1

Error-------------------------------------

    cell.lblText.text = arrayFullName[indexPath.row]


 Error -----------------------------------

    return cell as DovizCell1

}

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

    return 94

}
}

1 个答案:

答案 0 :(得分:0)

尝试将此添加到您的代码中

let cell:DovizCell? = tableView.dequeueReusableCell(withIdentifier: "cellDoviz",for:indexPath) as! DovizCell
if cell == nil {
     cell = NSBundle.mainBundle().loadNibNamed("DovizCell").first as! DovizCell
}

您的DovizCell可能是nil,这就是代码崩溃的原因