2016-10-28 2 views
0

Ich benutze UITableView für einige Daten anzeigen. Aber ich habe die Datenquellen- und Delegiertenmethoden in eine andere Klasse getrennt. Daher hat mein TableViewController keine Delegate-Methoden. Sie sind in getrennter Klasse.TableView Reload Daten ruft nicht CellForRowAtIndexPath

Wenn also die Tabellenansicht zum ersten Mal geladen wird, lädt sie die Werte. aber wenn ich tableView.reloadData auf einen Knopfklick einer Zelle verwende, ruft es nicht die cellForRawAtIndexPath Methode auf. Aber es ruft numberOfRawsInSection und andere Methoden auf.

Hier ist mein Controller-Klasse

class AdvancedSearch: UITableViewController , PopOverViewDelegate{ 

@IBOutlet var model: AdvancedSearchDataModel! 
let dataModel = AdvancedSearchDataModel() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.registerNib(UINib(nibName: "AgeCell", bundle: nil), forCellReuseIdentifier: "AgeCell") 
    tableView.registerNib(UINib(nibName: "ExperienceCell", bundle: nil), forCellReuseIdentifier: "ExperienceCell") 
    tableView.registerNib(UINib(nibName: "CityCell", bundle: nil), forCellReuseIdentifier: "CityCell") 
    tableView.registerNib(UINib(nibName: "StateCell", bundle: nil), forCellReuseIdentifier: "StateCell") 
    tableView.registerNib(UINib(nibName: "DateTimeCell", bundle: nil), forCellReuseIdentifier: "DateTimeCell") 
    tableView.registerNib(UINib(nibName: "GenderCell", bundle: nil), forCellReuseIdentifier: "GenderCell") 
    tableView.registerNib(UINib(nibName: "ResultButtonCell", bundle: nil), forCellReuseIdentifier: "ResultButtonCell") 

    //tableView.dataSource = model 
    //tableView.delegate = model 
    tableView.separatorColor = UIColor.grayColor() 
    tableView.allowsSelection = false 
    tableView.separatorStyle = UITableViewCellSeparatorStyle.None 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) { 
    tableView.reloadData() 
} 




// MARK: - DropDown PopOver Delegates 

func valueOfTheFieldWhenSelect(tableViewCell: UITableViewCell, value: String, indexPath: NSIndexPath) { 

     let data = AdvancedSearchDataModel() 
     data.buttonTitle = value 
     self.tableView.delegate = data 
     self.tableView.dataSource = data 
     self.tableView.reloadData() 

} 

Hier meine Datenquelle und Delegierten in Seperate Klasse

class AdvancedSearchDataModel: NSObject , UITableViewDataSource , UITableViewDelegate , AdvanceSearchDropDownButtonDelegate , UIPopoverPresentationControllerDelegate { 


let data = ["Age","Experience","City" , "State" , "Gender" , "Date and Time"] 

// set this proprty in PopOverViewDelegate method 
var buttonTitle : String? 




// MARK: - TableView Datasource 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    print("Count : \(data.count)") 
    return data.count + 1 
} 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    switch indexPath.row { 
    case 0: 
     let cell = tableView.dequeueReusableCellWithIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell 
     applyStyleForButton(cell.selectAge) 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 
     let additionalSeparator = UIView(frame: frame) 
     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 

     if buttonTitle != nil { 
      cell.selectAge.setTitle(buttonTitle, forState: .Normal) 
     } 

     cell.indexPath = indexPath 
     cell.dropDownDelegate = self 

     return cell 
    case 1: 
     let cell = tableView.dequeueReusableCellWithIdentifier("ExperienceCell", forIndexPath: indexPath) as! ExperienceCell 
     applyStyleForButton(cell.selectExperinceBtn) 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 

     let additionalSeparator = UIView(frame: frame) 

     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 
     return cell 
    case 2: 
     let cell = tableView.dequeueReusableCellWithIdentifier("CityCell", forIndexPath: indexPath) as! CityCell 
     applyStyleForButton(cell.cityButton) 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 

     let additionalSeparator = UIView(frame: frame) 

     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 
     return cell 
    case 3: 
     let cell = tableView.dequeueReusableCellWithIdentifier("StateCell", forIndexPath: indexPath) as! StateCell 
     applyStyleForButton(cell.stateButton) 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 

     let additionalSeparator = UIView(frame: frame) 

     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 
     return cell 
    case 4: 
     let cell = tableView.dequeueReusableCellWithIdentifier("GenderCell", forIndexPath: indexPath) as! GenderCell 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 

     let additionalSeparator = UIView(frame: frame) 

     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 
     return cell 
    case 5: 
     let cell = tableView.dequeueReusableCellWithIdentifier("DateTimeCell", forIndexPath: indexPath) as! DateTimeCell 
     applyStyleForButton(cell.datebutton) 
     applyStyleForButton(cell.timeButton) 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 

     let additionalSeparator = UIView(frame: frame) 

     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 
     return cell 
    case 6: 
     let cell = tableView.dequeueReusableCellWithIdentifier("ResultButtonCell", forIndexPath: indexPath) as! ResultButtonCell 
     let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2) 

     let additionalSeparator = UIView(frame: frame) 

     additionalSeparator.backgroundColor = UIColor.grayColor() 
     cell.addSubview(additionalSeparator) 
     return cell 
    default: 
     return UITableViewCell() 
    } 




} 



// MARK: - TableView Delegates 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 

    if indexPath.row == 5 { 
     return 150 
    }else{ 
     return 94 
    } 

} 

// MARK: - Styling 

func applyStyleForButton(button : UIButton){ 

    button.layer.borderColor = UIColor.grayColor().CGColor 
    button.layer.borderWidth = 1 
    button.layer.cornerRadius = 5 
    button.backgroundColor = UIColor.clearColor() 

} 


// MARK: - DropDown Delegates 


func openAgeDropDown(by button: UIButton, and cell: AgeCell,indexPath : NSIndexPath) 
{ 
    print("Age opened") 
    let tableViewController = DropDownController(style: .Plain, menuItems: ["10","20","30"],cell: cell , index: indexPath) 
    tableViewController.modalPresentationStyle = UIModalPresentationStyle.Popover 
    tableViewController.preferredContentSize = CGSizeMake(button.frame.width, button.frame.width) 

    // assigning the delegate in tableView to fill the textfield 
    tableViewController.delegate = AdvancedSearch() 

    let popoverPresentationController = tableViewController.popoverPresentationController 
    popoverPresentationController?.permittedArrowDirections = .Any 
    popoverPresentationController?.delegate = self 
    popoverPresentationController?.sourceView = button 
    popoverPresentationController?.sourceRect = button.bounds 

    UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(tableViewController, animated: true, completion: nil) 

} 


// MARK: - PopOver Delegates 

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { 
    return UIModalPresentationStyle.None 
}} 

Bitte Leite mich

+0

zu schaffen, was der Grund ist mehrere nibs eher zu verwenden als bequem alle Zellen entwerfen direkt in der Zieltabelle Aussicht? – vadian

+0

Ich habe viele benutzerdefinierte Zellen. Also kein Platz in der TableViewController selbst. Deshalb lade ich sie über Federn – Darshana

Antwort

1

Die Methoden nicht aufrufen, da jedes Mal wenn Sie eine ist erstellen neues Objekt von AdvancedSearchDataModel. Stattdessen weisen Sie delegate in viewDidLoad zu. braucht keine neue Aufgabe AdvancedSearchDataModel

in viewDidLoad

... 
tableView.separatorColor = UIColor.grayColor() 
tableView.allowsSelection = false 
tableView.separatorStyle = UITableViewCellSeparatorStyle.None 
tableView.delegate = self.model 
tableView.dataSource = self.model 

und in valueOfTheFieldWhenSelect

func valueOfTheFieldWhenSelect(tableViewCell: UITableViewCell, value: String, indexPath: NSIndexPath) { 

    self.model.buttonTitle = value 
    self.tableView.reloadData() 
} 
+0

Ich werde versuchen, Sie @sahil wissen zu lassen. Vielen Dank – Darshana

+1

okay hoffe, es wird funktionieren. – Sahil

+0

Es bringt mir einen Fehler. sagen, dass self.model ist – Darshana

Verwandte Themen