首页 文章

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

提问于
浏览
-1

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

我使用下面的代码进行单细胞扩展,但没有得到如何做多个细胞

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

我使用该代码扩展单个单元格,但如何做多个单元格,我没有得到

请帮助....提前谢谢

2 回答

  • 0

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

    对于扩展单元格,你必须在单击单元格时将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
    }
    
  • 3

    创建一个名为UITableViewController的TableViewController子类的新文件,并从storyboard中获取UITableViewController,并从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.
         }
    
      }
    

相关问题