2016-03-14 51 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

相关问题