Extensible UITableView IndexPath SWIFT Sections

Im pretty new for coding, I only know Swift. I found several tutorials for creating dropdown sections in a table. Basically it will be a television show, the headlines will be seasons and a drop-down list of episodes from each season.

I managed to get this work perfectly for what I want from https://github.com/fawazbabu/Accordion_Menu

All this looks good, however I need to be able to choose from the dropdowns. I added didSelectRowAtIndexPathonly with simple line printing to start. When I select a row, section or cell, random index paths are returned, the same row can be pressed a second time and returns a different value. I thought this was what I added to this problem. Therefore, I added didSelectRowAtIndexPathto the source code. This has the same problem.

I assume that this is because it is being used UIGestureRecogniser, as well as didSelectRowAtIndexPath. But I'm not sure what the alternative is.

Can someone tell me where I'm wrong, please?

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)
    }
+4
source share
1 answer

, , , , , . - , .

Github, , , UITapGestureRecognizer. , , .

AccordionMenu, , , .

, .

+5

Source: https://habr.com/ru/post/1599346/


All Articles