swift Swift collaspable或expandable tableview

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift collaspable或expandable tableview相关的知识,希望对你有一定的参考价值。

//
//  AccordionTableView.swift
//  PracticePro
//
//  Created by Muzahidul Islam on 5/26/15.
// 


import UIKit

class AccordionTableView: UITableViewController {
    var groupArray = [String]()
    var boolArray : [String]!
    var dataDic = [String : [String]]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Accordion Table"
        
        groupArray = ["A","B"]
        boolArray = [String](count: groupArray.count, repeatedValue: "0")
        
        let groupA = ["BANGLADESH","INDIA","PAKISTHAN","SOUTH AFRICA"]
        let groupB = ["AUSTRALIA","ENGLAND","SRILANKA","WEST INDIES"]
        
        dataDic["A"] = groupA
        dataDic["B"] = groupB
        
    }

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

    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 (boolForSec.toInt() != 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("reuseIdentifier", forIndexPath: indexPath) as! UITableViewCell
        
        let boolForSec = boolArray[indexPath.section] as String
        if (boolForSec.toInt() != 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 (boolForSec.toInt() != 0) {
            boolArray[section!] = "0"
            
        }else {
            boolArray[section!] = "1"
            
        }
        
        tableView.reloadSections(NSIndexSet(index: section!), withRowAnimation: UITableViewRowAnimation.Fade)
    }

   
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

}

以上是关于swift Swift collaspable或expandable tableview的主要内容,如果未能解决你的问题,请参考以下文章

如何从 OSX Swift 命令行工具或 shebang 脚本文件显示窗口?

Dart vs Swift

如何将 React-Native 代码与 Swift 或 Kotlin 联系起来?我应该用每种语言做啥?

找到开普勒方程的解 (iOS) (swift)

Swift-代理

无法识别的选择器发送到实例 - Swift