2016-06-21 10 views
3

Ich habe eine Tableview, die enter image description hereWie zeigt man die Anzahl der Zeilen im Abschnitt nach dem Tippen auf Abschnitt Kopfzeile in Swift?

einige

enthalten aber ich will es nur Abschnittsheader zeigen, und wenn ich auf Abschnitt Kopf tippen wird es alle Mitarbeiterdaten zeigen nur diesen Abschnitt contining.

enter image description here

Unten ist mein Code:

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return company.count 
} 



func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    return company[section].employees!.count 
} 


func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    if company[section].employees!.count < 1 { 

     return nil 
    } 

    return company[section].companyName 
} 


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

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! EmployeeTableViewCell 
    let employeeDetails = company[indexPath.section].employees!.allObjects as! [Employees] 

    cell.lblName.text = employeeDetails[indexPath.row].employeeName 
    cell.lblAddress.text = String(employeeDetails[indexPath.row].address!) 
    cell.lblAge.text = String(employeeDetails[indexPath.row].age!) 

    return cell 
} 

Dank!

Antwort

6

Fügen Sie zunächst eine Schaltfläche in der Kopfzeile hinzu, auf der Sie eine Funktion zum Erweitern der Zelle aufrufen können.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let titleHeader = company[section].companyName // Also set on button 
    let headerCell = UIView(frame: CGRectMake(0 , 0, tableView.frame.size.width , 40)) 
    headerCell.backgroundColor = UIColor.whiteColor() 

    let button = UIButton(frame: headerCell.frame) 
    button.addTarget(self, action: "selectedSectionStoredButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside) 
    button.setTitle(titleHeader, forState: UIControlState.Normal) 

    button.tag = section 
    headerCell.addSubview(button) 

    return headerCell 
    } 

Jetzt ausgewählte Kopfansicht überprüfen ButtonClicked

func selectedSectionStoredButtonClicked (sender : UIButton) { 
    if (selectedArray.containsObject(sender.tag)){ 
     selectedArray.removeObject(sender.tag) 
    }else{ 
     //selectedArray.removeAllObjects() // Uncomment it If you don't want to show other section cell . 
     selectedArray.addObject(sender.tag) 
    } 
    tableViewObj.reloadData() 
} 

HINWEIS: - Hier erklärte ich selectedArray als NSMutableArray Globale "tableViewObj" ist Ihr Tableview Objekt

Fahren Sie nun für die letzten Schritte fort. Änderungen in Ihrem numberOfRowsInSection

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if (selectedArray.containsObject(section)){ 
     return company[section].employees!.count 

    }else{ 
     return 0 
    } 
} 

Probieren Sie es aus, es wird funktionieren gut, wenn Sie irgendeine Verwirrung als Kommentar hinterlassen.

+0

Ich habe mySelected Array wie folgt deklariert: var selectedArray: NSMutableArray! und ich bekomme einen Absturz beim Tippen auf Abschnittsüberschrift in dieser Zeile if (selectedArray.containsObject (sender.tag)) { –

+0

deklarieren Array wie folgt var selectedArray: NSMutableArray = [] –

+0

@ S.Barti Ich habe eine Zeile in selectedSectionStoredButtonClicked Check hinzugefügt es. Wenn Sie dies auskommentieren als andere Abschnitt wird Zeile ausblenden –

1

erstellen varialbe wie

var hiddenSections: [Int] = [] 

// Tableview

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return tempContact.count 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if hiddenSections.contains(section) { 
     return 0 
    } 
    return tempContact[section].count 
} 

// ein headerCell erstellen Etikett hinzufügen und einen Knopf .do es in Storyboard

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let header = tableView.dequeueReusableCellWithIdentifier("HeaderCell")! as! HeaderCell 

    header._lblName.text = tempContact[section].NAME 
    header._btnExpand.addTarget(self, action: "hideSection:", forControlEvents: .TouchUpInside) 
    return header.contentView 
} 

// Taste Klicken Sie auf

func hideSection(sender: UIButton) { 
    if hiddenSections.contains(sender.tag) { 
     hiddenSections.removeAtIndex(hiddenSections.indexOf(sender.tag)!) 
     _tableView.reloadSections(NSIndexSet(index: sender.tag), withRowAnimation: .Automatic) 
     _tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: sender.tag), atScrollPosition: UITableViewScrollPosition.Bottom, animated: true) 
    } 
    else { 
     hiddenSections.append(sender.tag) 
     _tableView.reloadSections(NSIndexSet(index: sender.tag), withRowAnimation: .Automatic) 
    } 
}