在swift中点击节标题后,如何显示节中的行数?

在swift中点击节标题后,如何显示节中的行数?,swift,tableview,Swift,Tableview,我有一个tableView,其中包含一些 但我希望它只显示部分标题,当我点击部分标题时,它将显示仅包含该部分的所有员工详细信息 下面是我的代码: func numberOfSectionsInTableView(tableView: UITableView) -> Int { return company.count } func tableView(tableView: UITableView, numberOfRowsInSection section: Int)

我有一个tableView,其中包含一些

但我希望它只显示部分标题,当我点击部分标题时,它将显示仅包含该部分的所有员工详细信息

下面是我的代码:

 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
}
func numberOfSectionsInTableView(tableView:UITableView)->Int{
返回公司。计数
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
退货公司[部分]。员工数
}
func tableView(tableView:UITableView,titleForHeaderInSection:Int)->String?{
如果公司[部门]。员工!。计数<1{
归零
}
返回公司[部分]。公司名称
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
让cell=tableView.dequeueReusableCellWithIdentifier(“cell”)作为!EmployeeTableViewCell
让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!)
返回单元
}

谢谢

首先在页眉视图上添加一个按钮,您可以在该按钮上调用扩展单元格的函数

    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
    }
现在选中按钮上的选定标题视图

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()
}
注意:-这里我声明selectedArrayNSMutableArray全局“tableViewObj”是您的tableview对象

现在进行最后的步骤。更改您的行数部分

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

    }else{
        return 0
    }
}

试试看,如果你有任何困惑,就可以留下评论

创建一个类似于

 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
}
//创建一个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
}
//按钮点击

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)
    }
 }

我这样声明了mySelected数组:var selectedArray:NSMutableArray!如果(selectedArray.containsObject(sender.tag)){像这样声明数组变量selectedArray:NSMutableArray=[],我在这一行点击节头时会崩溃@S.Bharti我在selectedSectionStoredButtonClicked中添加了一行。如果您要取消注释,则其他部分将隐藏行。我已经完成了,但再次感谢您的宝贵时间。@Jogendra.Com。我想在swift中点击tableview标题部分添加行。如何实现此目的,请指导我,因为其他部分不同高度。