单击单元格上的按钮后,将UIView添加到确定的TableViewCell时出现问题

时间:2019-05-11 14:06:23

标签: swift uitableview uiview

在用户单击TableViewCell内的按钮后,我很难遮盖特定的TableViewCell,实际上,我的代码可以很好地遮盖特定的TableViewCell,但其他所有单元格都可以正常工作,而且我似乎无法理解为什么。我还有其他答案,但是它们是用目标C编写的,与这个特定问题不太相似。我正在使用字典以格式存储数据 [整数:[图像,标题,描述,价格,所选内容]],整数从0到n。

extension DishesViewController : UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dishesDictionary.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    tableView.register(UINib(nibName: "DishesTableViewCell", bundle: .main), forCellReuseIdentifier: "dishesTableViewCell")

    let cell = tableView.dequeueReusableCell(withIdentifier: "dishesTableViewCell", for: indexPath) as! DishesTableViewCell

    let width = cell.frame.width
    let height = cell.frame.height
    //REMEMBER TO REPOSITION THE RATIOS FOR A BETTER DESIGN WHEN GIVEN MORE DESIGN 
    //Sets the image position of the cells
    cell.dishImage.center.x = width * 0.1739
    cell.dishImage.center.y = height * 0.4933
    cell.dishImage.frame.size = CGSize(width: width * 128/414, height: height * 128/150)
    cell.dishImage.layer.cornerRadius = height * 0.1
    cell.dishImage.image = dishesDictionary[indexPath.item]![0] as? UIImage

    //Sets the position of the title and text
    cell.dishName.frame = CGRect(x: width * 156/414, y: height * 21/150, width: width * 241/414, height: height * 20/150)
    cell.dishName.text = dishesDictionary[indexPath.item]![1] as? String

    //Sets position of description and description text
    cell.dishDescription.frame = CGRect(x: width * 156/414, y: height * 49/150, width: width * 190/414, height: height * 59/150)
    cell.dishDescription.text = dishesDictionary[indexPath.item]![2] as? String

    //Sets position and text of price label
    cell.dishPrice.frame = CGRect(x: width * 156/414, y: height * 117/150, width: width * 190/414, height: height * 21/150)
    cell.dishPrice.text = ("\(dishesDictionary[indexPath.item]![3])€")

    //Sets position of addItemButton
    cell.addDishButton.center.x = width * 374/414
    cell.addDishButton.center.y = height * 78/150
    cell.addDishButton.frame.size = CGSize(width: width * 22/414, height: height * 22/150)
    cell.addDishButton.tag = indexPath.item
    cell.addDishButton.addTarget(self, action: #selector(self.addButtonPressed(sender:)), for: .touchUpInside)

    return cell
}

@objc func addButtonPressed(sender: UIButton) {

    let numberOfItemsInCart = dishesDictionary[sender.tag]![4] as! Bool
    let index = sender.tag

    if numberOfItemsInCart == false {
        print("MODIFIED \(dishesDictionary[index]![1])")

        let indexPath = IndexPath(item: index, section: 0)
        print("Index path : \(indexPath)")

        guard let cell = self.tableViewDishes.cellForRow(at: indexPath) else {return}
        print("Cell: \(cell)") //Returning every other cell, WRONG

        let opaqueView = UIView()
        opaqueView.frame = cell.frame
        opaqueView.center.x = cell.center.x
        opaqueView.center.y = cell.center.y
        opaqueView.backgroundColor = UIColor.black.withAlphaComponent(0.7)

        cell.addSubview(opaqueView)
        dishesDictionary[index]![4] = true
        tableViewDishes.reloadData()
    }

0 个答案:

没有答案