用swift制作简单的手风琴桌面视图?

用swift制作简单的手风琴桌面视图?,swift,tableview,accordion,Swift,Tableview,Accordion,有没有什么方法可以让我在swift中创建一个简单的手风琴视图,就像在日历事件创建时一样?我不想使用其他第三方库以及其他代码 我在github和google上找到了很多答案。但是,还是不符合我的要求 实际上我想添加两个表视图 第一个是展示城市的部分,如(纽约、洛杉矶、拉斯维加斯等) 当我点击其中一个城市时,它会在tableview中显示商店地址,这意味着有很多商店 所有存储和数据都将从json获得 我想做的手风琴视图和iOS上的日历应用程序一样简单。但是,我要插入到两个tableView中的数据(

有没有什么方法可以让我在swift中创建一个简单的手风琴视图,就像在日历事件创建时一样?我不想使用其他第三方库以及其他代码

我在github和google上找到了很多答案。但是,还是不符合我的要求

实际上我想添加两个表视图

第一个是展示城市的部分,如(纽约、洛杉矶、拉斯维加斯等)

当我点击其中一个城市时,它会在tableview中显示商店地址,这意味着有很多商店

所有存储和数据都将从json获得

我想做的手风琴视图和iOS上的日历应用程序一样简单。但是,我要插入到两个tableView中的数据(节头和每个节内的内部记录)是动态的

有什么帮助吗?请引导我,帮助我

更新:请看一看

试试这个:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    arrayForBool = ["0","0","0"]
    sectionTitleArray = ["Pool A","Pool B","Pool C"]
    var tmp1 : NSArray = ["New Zealand","Australia","Bangladesh","Sri Lanka"]
    var string1 = sectionTitleArray .objectAtIndex(0) as? String
    [sectionContentDict .setValue(tmp1, forKey:string1! )]
    var tmp2 : NSArray = ["India","South Africa","UAE","Pakistan"]
    string1 = sectionTitleArray .objectAtIndex(1) as? String
    [sectionContentDict .setValue(tmp2, forKey:string1! )]


    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")


}

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

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


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    if(arrayForBool .objectAtIndex(section).boolValue == true)
    {
        var tps = sectionTitleArray.objectAtIndex(section) as! String
        var count1 = (sectionContentDict.valueForKey(tps)) as! NSArray
        return count1.count
    }
    return 0;
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "ABC"
}

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

    return 50
}

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

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if(arrayForBool .objectAtIndex(indexPath.section).boolValue == true){
        return 100
    }

    return 2;
}

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

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

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

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

    return headerView
}

func sectionHeaderTapped(recognizer: UITapGestureRecognizer) {
    println("Tapping working")
    println(recognizer.view?.tag)

    var indexPath : NSIndexPath = NSIndexPath(forRow: 0, inSection:(recognizer.view?.tag as Int!)!)
    if (indexPath.row == 0) {

        var collapsed = arrayForBool .objectAtIndex(indexPath.section).boolValue
        collapsed       = !collapsed;

        arrayForBool .replaceObjectAtIndex(indexPath.section, withObject: collapsed)
        //reload specific section animated
        var range = NSMakeRange(indexPath.section, 1)
        var sectionToReload = NSIndexSet(indexesInRange: range)
        self.tableView .reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
    }

}

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

    let CellIdentifier = "Cell"
    var cell :UITableViewCell
    cell = self.tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as! UITableViewCell

    var manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue

    if (!manyCells) {
        //  cell.textLabel.text = @"click to enlarge";
    }
    else{
        var content = sectionContentDict .valueForKey(sectionTitleArray.objectAtIndex(indexPath.section) as! String) as! NSArray
        cell.textLabel?.text = content .objectAtIndex(indexPath.row) as? String
        cell.backgroundColor = UIColor .greenColor()
    }

    return cell
}

@TechBee提供的答案对于那些不想使用节和单元格的人来说,使用节的效果很好

使用
UITableView
可以以非常简单的方式实现Swift中的手风琴菜单,只要有两个单元格,一个用于父单元格,另一个用于子单元格,并随时跟踪展开或折叠的单元格,因为每次展开或折叠新单元格时,都可以更改
indexPath.row

使用函数并始终在调用
tableView.beginUpdate()
tableView.endUpdates()
的块内,并更新数据源中的项目总数或模拟其更改,我们可以通过包含动画的非常简单的方式在
UITableView
中插入删除新单元格


我已经在Github中实现了一个存储库,以一种简单易懂的方式使用Swift和
UITableView
实现了上述所有内容。它允许多个单元格展开或一次只展开一个单元格。

请添加一个屏幕截图以更好地了解您。我已经更新了这个问题。一个城市可能会有许多不同的店铺地址。我知道这个例子。这是github提供的。但是,我之所以问这个问题,是因为我想添加来自realm swift object的动态数据。如果您需要知道,我将更新问题。仅更改领域的数据部分。您的业务逻辑不应该改变。好的,我会试试。如果不行,我会让您知道。还有其他方法吗?如日历事件添加?我不能设置标题视图的自定义高度和宽度。我如何在标题视图单元格禁用分隔符?如何在swift中设置标题视图的左右间距?请看一看:只想这样设置标题视图。不仅仅是整个tableview。frame。size。width。我需要标题视图的左、右、顶部和按钮的一些间距。谢谢你简单易懂的方式。谢谢你的帮助customization@6245Htarwara当然甚至您也可以在存储库中看到我在答案中给出的示例,以查看它的实际应用:)