如何在swift中展开UITableview单元而不折叠其他单元格(多单元格展开)

时间:2016-03-14 13:50:54

标签: ios swift

当我们点击表格视图单元格时,它会展开,但如果其他cells已经展开,则它们不会崩溃意味着我们可以看到所有cells一次展开..

我使用以下代码进行单cell次扩展,但没有获取如何处理多个cell

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

        if indexPath.section == 2 && indexPath.row == selectedRowIndex && thereIsCellTapped {
            switch indexPath.row{
            case 0:
                return 119
            case 1:
                return 93
            case 2:
                return 117
            case 3:
                return 135
            case 4:
                return 93
            case 5:
                return 230
            default:
                return 140
            }
        }
        return 55
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let currentCell = tableView.cellForRowAtIndexPath(indexPath) as! PatientDetailsTableViewCell

        if indexPath.section == 2 {
        self.tableView.cellForRowAtIndexPath(indexPath)?.backgroundColor = UIColor.whiteColor()
        if self.selectedRowIndex != -1 {
            self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: self.selectedRowIndex, inSection: 2))?.backgroundColor = UIColor.whiteColor()
        }

        if indexPath.section == 2 && selectedRowIndex != indexPath.row {
            self.thereIsCellTapped = true
            self.selectedRowIndex = indexPath.row

            currentCell.lblRightArrow.text = "P"
            print(selectedRowIndex)
        }
        else {
            // there is no cell selected anymore
            self.thereIsCellTapped = false
            self.selectedRowIndex = -1
            currentCell.lblRightArrow.text = "p"
        }
        self.tableView.beginUpdates()
        self.tableView.endUpdates()
        }
    }

我使用该代码展开单cell但如何处理多个cell我没有得到

请帮助....提前致谢

2 个答案:

答案 0 :(得分:3)

经过太多工作后,我得到了扩展多个细胞的解决方案,所以我在这里分享帮助他人......

对于扩展单元格,你必须在单击单元格时将indexPath存储在数组中,然后在tableview委托方法中使用该数组的高度。我的代码在下面..

var selectedIndexPath : NSIndexPath?
var indexPaths : Array<NSIndexPath> = []
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    selectedIndexPath = indexPath
    if !indexPaths.contains(selectedIndexPath!){
            indexPaths += [selectedIndexPath!]
    }
    else {
        let index = indexPaths.indexOf(selectedIndexPath!)
        indexPaths.removeAtIndex(index!)
    }
    tableView.beginUpdates()
    tableView.endUpdates()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPaths.count>0 {
        if indexPaths.contains(indexPath){
            return 200
        }
        else {
            return 50
        }
    }
      return 50
}

答案 1 :(得分:0)

创建一个名为TableViewController子类UITableViewController的新文件,并从UITableViewController获取storyboard,并从Xcode的Identity Inspector中分配类TableViewController,并命名为小区标识符为Cell。然后实现下面的类:

class TableViewController: UITableViewController {

var groupArray = [String]()
var boolArray : [String]!
var dataDic = [String : [String]]()
override func viewDidLoad() {
 super.viewDidLoad()
groupArray = ["A","B","C"]
boolArray = [String](count: groupArray.count, repeatedValue: "0")
let groupA = ["CELL ONE","CELL TWO","CELL THREE","CELL FOUR","CELL FIVE"]
let groupB = ["CELL ONE","CELL TWO","CELL THREE","CELL FOUR","CELL FIVE"]
let groupC = ["CELL ONE","CELL TWO","CELL THREE","CELL FOUR","CELL FIVE"]
dataDic["A"] = groupA
dataDic["B"] = groupB
dataDic["C"] = groupC

}

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

override func tableView(tableView: UITableView, numberOfRowsInSection section:   Int) -> Int{
 let boolForSec = boolArray[section] as String
 if (Int(boolForSec) != 0) {
   var arr = dataDic[groupArray[section]]
   return arr!.count
 }else {
   return 0
 }
}

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

 let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:  indexPath) as! UITableViewCell

 let boolForSec = boolArray[indexPath.section] as String
 if (Int(boolForSec) != 0) {
   var arr : [String] = dataDic[groupArray[indexPath.section]]!
   cell.textLabel?.text = arr[indexPath.row] as String
 }else {

 }

 // cell.textLabel?.text = "row \(indexPath.row)"
 return cell
}

 override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

  return 50
 }

 override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
   return 1
 }

 override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, 40))
   headerView.backgroundColor = UIColor.blueColor()
   headerView.tag = section

  let headerString = UILabel(frame: CGRect(x: 10, y: 10, width:     tableView.frame.size.width-10, height: 30)) as UILabel
  headerString.text = groupArray[section] as String
  headerView .addSubview(headerString)

  let headerTapped = UITapGestureRecognizer (target: self,    action:"sectionHeaderTapped:")
   headerView .addGestureRecognizer(headerTapped)

      return headerView
  }

  func sectionHeaderTapped(tapped: UITapGestureRecognizer){

     let section = tapped.view?.tag
     let boolForSec = boolArray[section!] as String
      if (Int(boolForSec) != 0) {
         boolArray[section!] = "0"

      }else {
         boolArray[section!] = "1"

      }

   tableView.reloadSections(NSIndexSet(index: section!), withRowAnimation:  UITableViewRowAnimation.Fade)
    }

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

  }