快速制作简单的 Accordion TableView?

Posted

技术标签:

【中文标题】快速制作简单的 Accordion TableView?【英文标题】:Making Simple Accordion TableView in swift? 【发布时间】:2016-05-25 11:13:02 【问题描述】:

有什么方法可以让我快速制作简单的 Accordion View,就像 Calendar Event Create 中的那样?我不想使用其他第三方库以及其他代码。

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

其实我想加两个table view。

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

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

所有的 store 和 data 都会从 json 中获取。

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

有什么帮助吗?请指导我,帮助我。

更新:请看一下

【问题讨论】:

能否请您添加截图以更好地了解您 我已经更新了问题。一个城市可能有很多店铺地址。 【参考方案1】:

@TechBee 提供的答案适用于那些对不使用部分和使用单元格感兴趣的人使用部分。

Accordion Menu 在 Swift 中的实现可以使用 UITableView 以一种非常简单的方式实现,只需要两个单元格,一个用于父单元格,另一个用于子单元格,并且每时每刻跟踪单元格展开或折叠,因为它可以在每次展开或折叠新单元格时更改indexPath.row

tableView.beginUpdates()tableView.endUpdates()的调用块中使用函数insertRowsAtIndexPaths(_:withRowAnimation:)deleteRowsAtIndexPaths(_:withRowAnimation:),并更新数据源中的项目总数或模拟它的变化,我们可以实现插入删除新的UITableView 中的单元格以非常简单的方式包含动画。

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

【讨论】:

感谢您的简单易懂的方式。感谢您的一些定制 @6245Htarwara 当然,即使您可以在存储库中看到我在答案中放置的示例以查看它的实际效果:)【参考方案2】:

试试这个:

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

【讨论】:

我知道那个例子。那是来自 github。但是,我问这个的原因是因为我想从领域 swift 对象中添加数据动态。如果你需要知道,我会更新问题。 仅更改领域的数据部分。您的业​​务逻辑不应改变。 好的,我试一试。如果不行,我会告诉你的。还有其他方法吗?比如添加日历事件? 我无法为标题视图设置自定义高度和宽度。如何在标题视图单元格中禁用分隔符? 如何快速设置标题视图左右间距?请看一下:puu.sh/nf7cc/168be859ff.png.I 只想设置这样的标题视图。不仅仅是整个 tableview.frame.size.width。我需要在标题视图的左、右、顶部和按钮之间留一些间距。

以上是关于快速制作简单的 Accordion TableView?的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap Accordion 在集成到 CMS 时恶意滑动

Jquery Accordion 展开全部 全部折叠

unity快速了解游戏制作流程-制作九宫格简单游戏demo

快速的制作一张简单报表

Jquery Accordion 小部件 - 删除图标和填充

jQuery Accordion - 需要当前选定内容部分的索引