2017-04-10 85 views
1

我嘗試了很多次,但它不起作用。如何將json數據傳遞到collapsableheader tableview控制器

錯誤:

/Users/admin/Desktop/Bkk 2/bkk/OfferDetailViewController.swift:93:55: Cannot convert value of type '[Deal]' to expected argument type '[String]'

代碼:

var deals = [Deal]() 

var sections = [Section]() 


sections = [ 
     Section(name: "Select from option", items:deals), 
     Section(name: "merchant Details", items: [ForyouString, "iPad Air 2", "iPad mini 4", "Accessories"]), 
     Section(name: "How to use deals", items: ["iPhone 6s", "iPhone 6", "iPhone SE", "Accessories"]), 
     Section(name: "things to remember", items: ["exchange for cash not allowed"]), 
     Section(name: "Cancelation policy", items: ["Once bought cannot exchange"]), 
     Section(name: "what you get", items: ["Capacity buliding courses"]) 
    ] 
func parseDeals(data: NSData) -> [Deal]{ 
    var deals = [Deal]() 

    do{ 
     let jsonresult = try JSONSerialization.jsonObject(with: data as Data, options: .mutableContainers) as? NSDictionary 

     let jsondeals = jsonresult?["deals"] as! [AnyObject] 
     for jsondeal in jsondeals{ 
      let deal = Deal() 
      deal.deals_name = jsondeal["deals_name"] as! String 
      deal.image_url = jsondeal["image_url"] as! String 
      deal.actual_price = jsondeal["actual_price"] as! String 
      deal.discounted_price = jsondeal["discounted_price"] as! String 
      deal.discounted_percentage = jsondeal["discounted_percentage"] as! String 
      deal.max_purchase_per_customer = jsondeal["max_purchase_per_customer"] as! String 
      deal.qty_available = jsondeal["qty_available"] as! String 
      deal.valid_from = jsondeal["valid_from"] as! String 
      deal.valid_to = jsondeal["valid_to"] as! String 
      deals.append(deal) 

     } 
       } 
    catch{ 
     print(error) 

    } 
    return deals 
} 


func back(_ sender: Any) { 
    self.dismiss(animated: true, completion: nil) 
} 

func buttonClicked(){ 

    print("button Clicked") 

} 
func showLocalError(errorMsg: String,title:String = "Oops!") { 
    let appearance = SCLAlertView.SCLAppearance(
     showCloseButton: true 
    ) 
    let alertView = SCLAlertView(appearance: appearance) 
    alertView.showWarning(title, subTitle: errorMsg) 
} 


func numberOfSections(in tableView: UITableView) -> Int { 
    return sections.count 
} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return sections[section].items.count 
} 

// 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell? ?? UITableViewCell(style: .default, reuseIdentifier: "cell") 

    cell.textLabel?.text = sections[(indexPath as NSIndexPath).section].items[(indexPath as NSIndexPath).row] 
    return cell 
} 

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return sections[(indexPath as NSIndexPath).section].collapsed! ? 0 : 44.0 
} 

它顯示了錯誤的部分,當我通過交易,它顯示的錯誤,我找不到在哪裏我沒有錯,我需要如何通過交易數組。

+2

你似乎使用Swift 3,避免使用所以避免'NSDictionary'和其他「太Objective-C」的東西。 – Larme

+0

'cell.textLabel?.text = sections [(indexPath as NSIndexPath).section] .items [(indexPath as NSIndexPath).row]'如果有'section == 0',那麼你有一個Deals對象數組,不是一個字符串數組。 – Larme

+0

它顯示部分= [ 部分(名稱:「從選項中選擇,項目:交易)錯誤,當我在這裏通過交易時,它顯示錯誤 – udaykumar40

回答

2

該錯誤消息是很清楚的:

在線路

Section(name: "Select from option", items:deals) 

要傳遞Deal對象數組作爲第二個參數,而不是的String預期陣列,所述Section初始值是

init(name: String, items: [String]) 
+0

即使它顯示錯誤/ Users/admin/Desktop/B 2/By/OfferDetailViewController.swift:93:56:無法將類型'[String] .Type'(又名'Array .Type')的值轉換爲預期的參數類型'[String]' – udaykumar40

+0

你是如何改變生產線的?我懷疑你寫了'Section(name:「Select from option」,items:[String])'這是錯誤的。您必須傳遞類似於其他行中的字符串數組,或者傳遞一個空數組,然後寫入Section(name:「Select from option」,items:[String]())' – vadian

+0

sections = [0126]名稱:「SelectFromOptions」,項目:[字符串]) – udaykumar40

相關問題