可扩展部分UITableView IndexPath SWIFT

时间:2015-07-23 15:15:12

标签: swift uitableview didselectrowatindexpath expandable

我对编码很新,我只知道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)
    }

1 个答案:

答案 0 :(得分:5)

我不知道为什么你需要用你喜欢的部分去做,你可以用正常的细胞来实现,根本不需要复杂化。只需区分子细胞的亲本细胞即可。

我在Github中有一个存储库可以实现你想要的,而不使用UITapGestureRecognizer部分。我会尽快更新项目,以便在下拉列表中更好地提升性能和更深层次。

您可以与AccordionMenu 联系,随时发布您需要的任何内容。

我希望这对你有所帮助。

相关问题