2016-03-14 83 views
-2

當我們點擊表格視圖單元格時,它將展開,但如果其他單元格已經展開,它們將不會崩潰意味着我們可以看到一次展開的所有單元格..如何在不崩潰的情況下展開UITableview單元其他單元格(多單元展開)

我用下面的單個細胞的代碼擴展,但沒有得到如何爲多小區做

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

我用那個鱈魚提前E對於擴大單細胞,但如何爲多個小區做我沒有得到

請幫助....感謝

+0

請發表您已經使用的代碼,而該代碼已用於嘗試解決該問題。 –

回答

2

太多的工作,我得到了解決,擴大多個小區,所以我在這裏分享幫助他人..

用於擴展蜂窩後,必須存放在數組中的indexPath在單擊單元格時,則使用數組中的tableview委託方法height.My代碼如下..

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 
} 
+0

謝謝tableView.beginUpdates(); tableView.endUpdates()!!!絕對忘了這個..它真的幫助我。 –

0

創建命名的UITableViewController的TableViewController子類一個新的文件,並採取從故事板一個UITableViewController並從Xcode的Identity Inspector中分配類TableViewController,並將單元格標識符命名爲「單元格」。然後實現類波紋管:

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

    } 
+0

您好Md.Muzahidul伊斯蘭你的解決方案是好的,但它不會爲我工作,因爲我有3個部分的自定義單元格和其中一個部分有5個單元格將擴大與他們的子視圖。所以我希望所有5個單元格都會一次展開... –

+0

嗨,我在viewDidLoad方法中做了一些更改。如你所說,現在三節和每節有五個單元格。 – iMuzahid

相關問題