2017-04-10 4 views
1

Ich versuche viele Male, aber es funktioniert nicht.Wie JSON-Daten zu Collapsableheader Tableview-Controller übergeben

Fehler:

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

Code:

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 
} 

Es zeigt Fehler an den Abschnitten, wenn ich Angebote passieren, dass es Fehler zeigt, kann ich nicht finden, wo ich Fehler gemacht habe, ich brauche, wie man Übergibt Deal-Array in Abschnitten.

+2

Sie scheint Swift 3 zu verwenden, vermeiden Sie die Verwendung So vermeiden Sie 'NSDictionary' und andere" zu Objective-C "Zeug. – Larme

+0

'cell.textLabel? .text = Abschnitte [(indexPath als NSIndexPath) .section] .items [(indexPath als NSIndexPath) .row]' Im Falle von 'section == 0' haben Sie ein Array von Deals-Objekten, kein Array von Strings. – Larme

+0

zeigt Fehler bei Sektionen = [ Abschnitt (Name: "Wählen Sie aus der Option", Elemente: Angebote), wenn ich hier Abschlüsse gibt es zeigt diesen Fehler – udaykumar40

Antwort

2

Die Fehlermeldung ist sehr klar:

In der Zeile

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

Sie eine Reihe von Deal Objekte als zweiten Parameter anstelle eines erwarteten Array von String sind vorbei, die Section initializer ist

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

selbst zeigt es Fehler/Benutzer/Admin/Desktop/B 2/By/OfferDetailViewController.Swift: 93: 56: Wert des Typs '[String] .Type' (auch bekannt als 'Array .Type') kann nicht zum erwarteten Argumenttyp konvertiert werden '[String]' – udaykumar40

+0

Wie hast du die Zeile geändert? Ich vermute, du hast 'Section (Name:" Wähle aus Option ", Elemente: [String])' geschrieben, was falsch ist. Sie müssen ein Array von Strings wie in den anderen Zeilen übergeben oder Sie übergeben ein leeres Array und schreiben dann Abschnitt (Name: "Select from option", Elemente: [String]()) ' – vadian

+0

sections = [ Section.init (Name: "SelectFromOptions", Elemente: [String]) – udaykumar40

Verwandte Themen