Swift 可扩展部分UITableView索引

Swift 可扩展部分UITableView索引,swift,uitableview,didselectrowatindexpath,expandable,Swift,Uitableview,Didselectrowatindexpath,Expandable,我对编码相当陌生,我只知道斯威夫特。我找到了一些教程,可以在表格中生成下拉部分。基本上,它将代表一个电视节目,标题将是季节和每个季节的剧集下拉列表 我设法让它完美地为我想要的东西工作 这一切看起来不错,但我需要能够从下拉列表中选择项目。我添加了didselectrowatinexpath,只需简单打印行即可。当我选择一行、节或单元格时,将返回随机索引路径,可以再次按下同一行并返回不同的值。我以为这只是我在这个问题上添加的东西。因此,我在原始代码中添加了didSelectRowAtIndexPat

我对编码相当陌生,我只知道斯威夫特。我找到了一些教程,可以在表格中生成下拉部分。基本上,它将代表一个电视节目,标题将是季节和每个季节的剧集下拉列表

我设法让它完美地为我想要的东西工作

这一切看起来不错,但我需要能够从下拉列表中选择项目。我添加了
didselectrowatinexpath
,只需简单打印行即可。当我选择一行、节或单元格时,将返回随机索引路径,可以再次按下同一行并返回不同的值。我以为这只是我在这个问题上添加的东西。因此,我在原始代码中添加了
didSelectRowAtIndexPath
。这也有同样的问题

我假设这是因为正在使用
uigesturecognizer
以及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)
    }

我不知道为什么你需要切片来做你想做的,你可以用普通细胞来实现,根本不需要复杂化。只需区分子细胞的父细胞即可

我在Github中有一个存储库,可以实现您想要的功能,而无需使用
UITapGestureRecognizer
。我会尽快更新项目,使其在下拉列表中具有更好的性能和更高的深度

您可以访问它,并随时发布您需要的任何内容


我希望这对你有帮助。

谢谢你,太好了@维克多·西格勒:你的项目看起来不错。如果您在其自述文件(如cocoapods)上添加实现过程,或者添加所有文件以在其他项目中使用您的表视图,那将非常好。@SakkeerHussain我将尝试添加一些功能,如cocoapods和Carthage,以便在接下来的几周内将其集成为一个库。我在等一段时间。谢谢你的建议