我收到以下错误:
2016-06-22 20:36:42.343016光纤[589:131756] *断言失败 - [UITableView _configureCellForDisplay:forIndexPath:],/ BuildRoot / Library / Cache / com.apple.xbs / Sources / UIKit的/ UIKit的-3575.10 / UITableView.m:7964 2016-06-22 20:36:42.345150光纤[589:131756] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView(; layer =; contentOffset:{0,0}; contentSize: {383,280}>)无法从其dataSource()获取单元格 ***第一次抛出调用堆栈: (0x184989980 0x183f844bc 0x184989854 0x1853adc84 0x18a85465c 0x18aa522bc 0x18aa523c0 0x18aa40924 0x18aa57084 0x18a7f740c 0x18a71058c 0x187c34d6c 0x187c29aac 0x187c2996c 0x187ba94fc 0x187bd07c4 0x187bd1148 0x184939954 0x184937584 0x184937a14 0x18486a2e4 0x18624315c 0x18a77e6fc 0x18a779438 0x100070c3c 0x18440c600) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)
来自以下代码:
import Foundation
import Parse
import UIKit
extension UIImageView {
public func imageFromUrl(_ urlString: String) {
if let url = URL(string: urlString) {
let request = URLRequest(url: url)
NSURLConnection.sendAsynchronousRequest(request, queue: OperationQueue.main()) {
(response: URLResponse?, data: Data?, error: NSError?) -> Void in
if let imageData = data as Data? {
self.image = UIImage(data: imageData)
}
}
}
}
}
class offersViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var offersView: UIView!
@IBOutlet weak var offerTitleLabel: UILabel!
@IBOutlet weak var smartCode: UILabel!
@IBOutlet weak var customerName: UILabel!
@IBAction func closeButton(sender: AnyObject) {
offersView.isHidden = true
}
var array: [String] = [String]()
var arrayImages: [String] = [String]()
override func viewDidLoad(){
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
self.array.removeAll(keepingCapacity: true)
self.arrayImages.removeAll(keepingCapacity: true)
let nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "cell")
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let weekDay = dateFormatter.string(from: date)
print(weekDay)
tableView.isHidden = true
let query = PFQuery(className:"offers")
query.whereKey("day", equalTo:"\(weekDay)")
query.findObjectsInBackground {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
let birthdayOffer = "£25 free credit on your Birthday!"
let birthdayOfferImage = "birthday"
self.array.append(birthdayOffer as String)
print(self.array)
self.arrayImages.append(birthdayOfferImage as String)
print(self.arrayImages)
for object in objects {
let query = PFQuery(className:"offers")
query.getObjectInBackground(withId: "\(object.objectId!)"){
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil && gameScore != nil {
let offertitle = gameScore!["offertitle"] as! String
let offerImage = gameScore!["imagename"] as! String
// Add birthday to array
self.array.append(offertitle as String)
print(self.array)
self.arrayImages.append(offerImage as String)
print(self.arrayImages)
}
self.tableView.reloadData()
self.tableView.isHidden = false
}
}
}
}
}
}
func tableView(_ tableView: UITableView, canFocusRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
var cell : UITableViewCell?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TblCell = self.tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! TblCell
let url = URL(string: "https://checkmyweave.co.uk/\(arrayImages[(indexPath as NSIndexPath).row]).png")
cell.offerImage.sd_setImage(with: url)
cell.offerTitleLabel.text = self.array[(indexPath as NSIndexPath).row]
let defaults = UserDefaults.standard()
let userID = defaults.string(forKey: "userID")!
let query = PFQuery(className:"members")
query.getObjectInBackground(withId: "\(userID)") {
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil && gameScore != nil {
let smartCode = gameScore!["smartcode"] as! String
let defaults = UserDefaults.standard()
let userName = defaults.string(forKey: "userName")!
self.smartCode.text = smartCode
self.customerName.text = userName
} else {
print(error)
}
}
return cell
}
func numberOfSections(_ tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print((indexPath as NSIndexPath).row)
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let weekDay = dateFormatter.string(from: date)
offerTitleLabel.text = self.array[(indexPath as NSIndexPath).row]
offersView.isHidden = false
print(offerTitleLabel.text)
let query = PFQuery(className:"offers")
query.whereKey("offertitle", equalTo:"\(offerTitleLabel.text!)")
query.whereKey("day", equalTo:"\(weekDay)")
query.findObjectsInBackground {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
for object in objects {
let query = PFQuery(className:"offers")
query.getObjectInBackground(withId: "\(object.objectId!)"){
(gameScore: PFObject?, error: NSError?) -> Void in
if error == nil && gameScore != nil {
let offerdesc = gameScore!["offerDesc"] as! String
print(offerdesc)
}}}}}}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 140
}
}
任何人都可以解释原因吗?
谢谢!