2016-06-21 4 views
3

私はいくつかのセクションヘッダーをタップした後、セクション内の行数を表示するにはどうすればいいですか?

enter image description here

が含まれているのtableViewを持っているが、私はそれが唯一のセクションヘッダーを表示したいと私はセクションヘッダをタップするときには、そのセクションのみをcontining、すべての従業員の詳細が表示されます。以下は

enter image description here

私のコードです:

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 
} 

ありがとう!

答えて

6

まず、ヘッダービューにボタンを追加して、セルを拡張するための関数を呼び出すことができます。

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 
    } 

は今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() 
} 

NOTE上で選択したヘッダビューを確認します -ここで私は、selectedArrayとしてNSMutableArrayのグローバル"tableViewObjは"

あなたのテーブルビューオブジェクトであると宣言しました今すぐ最終ステップを進めてください。あなたのnumberOfRowsInSection

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

    }else{ 
     return 0 
    } 
} 

あなたはコメントを残すよりも混乱を持っている場合は、正常に動作します、それを試してみてくださいに変更を加えること。

+0

私はこのようにmySelected配列を宣言しています:var selectedArray:NSMutableArray! (selectedArray.containsObject(sender.tag)){ –

+0

このような配列を宣言します。var selectedArray:NSMutableArray = [] –

+0

@ S.Bharti selectedSectionStoredButtonClickedチェックに1行追加しました。この行のセクションヘッダーをタップするとクラッシュします。それ。あなたがコメントアウトする場合は、他のセクションよりも行が隠れるようになります –

1

headerCellは、ラベルを追加し、ボタンはストーリーボード

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 
} 

//ボタンでそれを.doという作成

var hiddenSections: [Int] = [] 

//テーブルビュー

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 
} 

//などのvarialbeを作成します。クリック

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) 
    } 
} 
関連する問題