简体   繁体   中英

How to handle tap on row of 2nd tableView which is under the 1st table view cell in swift?

First Table View Image 1 ->

在此处输入图片说明

Second table view (Which is nested in 1st Table View) Image 2 ->

在此处输入图片说明

First row of nested table view working after tap but others are not working Image 3->

在此处输入图片说明

When I tap on 1st row of 1st tableView then it is working properly and when I tap on 1st row of second tableView then it is also working.

Problem is when I tap on 2nd row of 2nd tableView then it is not working.

Same as if second row of 1st table view is taped then in 2nd tableView only 2nd row is working when tap.

class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource {


    var selectedIndex = -1  
    var nestedSelectedIndex = -1 

    var nestedTableViewCellDataTitle : [String] = ["ABC123", "DEF456","GHI890","JKL145"]
    @IBOutlet weak var tableView: UITableView!


    override func viewDidLoad() {
        super.viewDidLoad()


    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 1{
            return 5
        }
        else{
            return nestedTableViewCellDataTitle.count
        }

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView.tag == 1{
            let cell = tableView.dequeueReusableCell(withIdentifier: "CartViewCell", for: indexPath) as! CartTableViewCell
            cell.nestedTableView.delegate = self
            cell.nestedTableView.dataSource = self

            return cell
        }else {
             let cell = tableView.dequeueReusableCell(withIdentifier: "NestedCartTableCell", for: indexPath) as! NestedCartTableCell
            cell.productTitle.text = nestedTableViewCellDataTitle[indexPath.row]

            return cell
        }

    }


    //Hide and Open when we tap on table view
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.tag == 1{
            if selectedIndex == indexPath.row{
                selectedIndex = -1
            }else{
                selectedIndex = indexPath.row
            }
            self.tableView.beginUpdates()

            self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic)

            self.tableView.endUpdates()
        }else if tableView.tag == 2{
            if nestedSelectedIndex == indexPath.row{
                nestedSelectedIndex = -1
            }else{
                nestedSelectedIndex = indexPath.row
            }
            self.tableView.beginUpdates()
            self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic)

            self.tableView.endUpdates()
        }

    }

    //Specify hight for both view
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView.tag == 1{
            if selectedIndex == indexPath.row{
                return 500

            }else{
                return 37
            }
        }else{
            if nestedSelectedIndex == indexPath.row{
                return 410
            }else{
                return 106
            }
        }

    }

Create new variable of type UITableView. Store nested tableView form 1st TableView cell and then reload this table when tap on row on nested tableView.

class CartVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

var selectedIndex = -1  
var nestedSelectedIndex = -1 

var nestedTableViewCellDataTitle : [String] = ["ABC123", "DEF456","GHI890","JKL145"]


var newTableView = UITableView() **//This is the key (Solution Line 1)**



 var requestForMatchineStyleImage : [String] =["BAGS","BANGLES","BELTS","BRACELETS","CUFFLINKS","EARRINGS","FINGER-RINGS","HEADBANDS","KEYRINGS","NECKLACES","STRINGS"]


@IBOutlet weak var tableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()


}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView.tag == 1{
        return 5
    }
    else{
        return nestedTableViewCellDataTitle.count
    }

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView.tag == 1{
        let cell = tableView.dequeueReusableCell(withIdentifier: "CartViewCell", for: indexPath) as! CartTableViewCell
        cell.nestedTableView.delegate = self
        cell.nestedTableView.dataSource = self

        newTableView = cell.nestedTableView //**This is the key (Solution Line 2)**

        return cell
    }else {
         let cell = tableView.dequeueReusableCell(withIdentifier: "NestedCartTableCell", for: indexPath) as! NestedCartTableCell
        cell.productTitle.text = nestedTableViewCellDataTitle[indexPath.row]

        return cell
    }

}


//Hide and Open when we tap on table view
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    //let cell = tableView.cellForRow(at: indexPath)


    if tableView.tag == 1{
        if selectedIndex == indexPath.row{
            selectedIndex = -1
        }else{
            selectedIndex = indexPath.row
        }
        self.tableView.beginUpdates()

        self.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic)

        self.tableView.endUpdates()
    }else if tableView.tag == 2{
        if nestedSelectedIndex == indexPath.row{
            nestedSelectedIndex = -1
        }else{
            nestedSelectedIndex = indexPath.row
        }
        self.newTableView.beginUpdates() **//This is the key (Solution Line 3)**
        self.newTableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic) **//This is the key (Solution Line 4)**

        self.newTableView.endUpdates()**//This is the key (Solution Line 5)**
    }

}



//Specify hight for both view
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if tableView.tag == 1{
        if selectedIndex == indexPath.row{
            return 500

        }else{
            return 37
        }
    }else{
        if nestedSelectedIndex == indexPath.row{
            return 430
        }else{
            return 106
        }
    }

}


@IBAction func backArrowBtnAction(_ sender: Any) {
    dismiss(animated: true
        , completion: nil)
}


@IBAction func backTextBtnAction(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}



static func storyboardInstance() -> CartVC? {
    let storyboard = UIStoryboard(name: "CartSt", bundle: nil)
    return storyboard.instantiateInitialViewController() as? CartVC
}



override var prefersStatusBarHidden: Bool {
    return true
}

}

extension CartVC : UICollectionViewDelegate, UICollectionViewDataSource{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return requestForMatchineStyleImage.count }

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCellOfRFQ", for: indexPath) as! RFQNestedCollectionViewCell

    return cell
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM