【问题标题】:Expandable Sections UITableView IndexPath SWIFT可扩展部分 UITableView IndexPath SWIFT
【发布时间】:2015-10-14 00:03:52
【问题描述】:

我对编码很陌生,我只知道 Swift。我找到了几个教程来在表格中生成下拉部分。基本上它将代表一个电视节目,标题将是季节和每个季节的剧集的下拉列表。

我设法从https://github.com/fawazbabu/Accordion_Menu 得到了我想要的完美工作

这一切看起来都不错,但是我需要能够从下拉项目中进行选择。我添加了didSelectRowAtIndexPath,只需简单地打印行开始。当我选择一行、部分或单元格时,会返回随机索引路径,可以再次按下同一行并返回不同的值。我认为这只是我在问题中添加的内容。所以我在原始代码中添加了didSelectRowAtIndexPath。这有同样的问题。

我认为这是因为使用了 UIGestureRecogniser 以及 didSelectRowAtIndexPath。但我不确定替代方案是什么。

谁能告诉我哪里出错了?

import UIKit
import Foundation


class test: UIViewController, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet var tableView: UITableView!
    var sectionTitleArray : NSMutableArray = NSMutableArray()
    var sectionContentDict : NSMutableDictionary = NSMutableDictionary()
    var arrayForBool : NSMutableArray = NSMutableArray()


    override func awakeFromNib() {
        super.awakeFromNib()

        tableView.delegate = self
        tableView.dataSource = self

        arrayForBool = ["0","0"]
        sectionTitleArray = ["Pool A","Pool B"]
        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.registerNib(UINib(nibName: "CategoryNameTableCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "CategoryNameTableCell")
    }

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

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

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("CategoryNameTableCell") as! CategoryNameTableCell
        cell.downArrow.hidden = false
        cell.backgroundColor = UIColor.blackColor()
        cell.tag = section
        cell.CategoryLabel.text = sectionTitleArray.objectAtIndex(section) as? String
        let cellTapped = UITapGestureRecognizer (target: self, action:"sectionHeaderTapped:")
        cell .addGestureRecognizer(cellTapped)
        return cell
    }
    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)
            var range = NSMakeRange(indexPath.section, 1)
            var sectionToReload = NSIndexSet(indexesInRange: range)
            self.tableView .reloadSections(sectionToReload, withRowAnimation:UITableViewRowAnimation.Fade)
            tableView.reloadData()
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = self.tableView.dequeueReusableCellWithIdentifier("CategoryNameTableCell") as! CategoryNameTableCell
        var manyCells : Bool = arrayForBool .objectAtIndex(indexPath.section).boolValue
        if (!manyCells) {
        } else {
            var content = sectionContentDict .valueForKey(sectionTitleArray.objectAtIndex(indexPath.section) as! String) as! NSArray
            cell.CategoryLabel.text = content .objectAtIndex(indexPath.row) as? String
            cell.backgroundColor = UIColor( red: 49/255, green: 46/255, blue:47/255, alpha: 1.0 )
        }

        return cell
    }
    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
        println(indexPath.row)
    }

【问题讨论】:

    标签: swift uitableview didselectrowatindexpath expandable


    【解决方案1】:

    我不知道为什么你需要部分来做你想做的事情,你可以用正常的细胞来实现,完全不需要复杂化。只需区分子单元的父单元即可。

    我在 Github 中有一个存储库,可以实现你想要的,而不使用 UITapGestureRecognizer 的部分。我会尽快尝试更新项目,以在下拉菜单中获得更好的性能和更多的深度。

    您可以通过AccordionMenu 联系它,并随时发布您需要的任何问题。

    希望对你有所帮助。

    【讨论】:

    • 谢谢你,太好了!
    • @Victor Sigler 你的项目看起来不错。如果您在其自述文件中添加实现过程(如 cocoapods)或应添加所有文件以在其他项目中使用您的表格视图,那就太好了。
    • @SakkeerHussain 我将在接下来的几周内尝试添加一些功能,例如 Cocoapods 和 Carthage,以便将其集成为一个库。我在等一段时间来做。感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多