Swift: I want to know what is the index path of the button that I pressed?

I have my own cell class below:

class SizeAndQuantityCellView:UITableViewCell
{

@IBOutlet weak var imageview: UIImageView!
@IBOutlet weak var plusButton4x4: UIButton!
@IBOutlet weak var plusButton4x6: UIButton!
@IBOutlet weak var plusButton5x7: UIButton!
@IBOutlet weak var plusButton8x10: UIButton!
@IBOutlet weak var minusButton4x4: UIButton!
@IBOutlet weak var minusButton4x6: UIButton!
@IBOutlet weak var minusButton5x7: UIButton!
@IBOutlet weak var minusButton8x10: UIButton!
@IBOutlet weak var quantity4x4: UILabel!
@IBOutlet weak var quantity4x6: UILabel!
@IBOutlet weak var quantity5x7: UILabel!
@IBOutlet weak var quantity8x10: UILabel!

let sizeAndQuantityController = SizeAndQuantityController()
@IBAction func plusButtonClick(sender: UIButton)
{
    let btnTag:Int = sender.tag
    let tableView = sender.superview!.superview?.superview as! UITableView
    let cellRow = tableView.indexPathForCell(self)?.row
    sizeAndQuantityController.plusButtonClick(btnTag,cellRow: cellRow!)
}

@IBAction func minusButtonClick(sender: UIButton)
{
    let btnTag:Int = sender.tag
    let tableView = sender.superview!.superview?.superview as! UITableView
    let cellRow = tableView.indexPathForCell(self)?.row
    sizeAndQuantityController.plusButtonClick(btnTag,cellRow: cellRow!)
}
}

What I want to do is when I press the plus button, the amount should increase by one, and when I click the minus button, it should decrease by one. Here is my controller class for this:

class SizeAndQuantityController
{
func plusButtonClick(tag:Int,cellRow:Int)
{
    switch tag
    {
    case 13:
        let quant = quantity4x4[cellRow]
        quantity4x4[cellRow] = quant+1
        break;
    case 14:
        let quant = quantity4x6[cellRow]
        quantity4x6[cellRow] = quant+1
        break;
    case 15:
        let quant = quantity5x7[cellRow]
        quantity5x7[cellRow] = quant+1
        break;
    case 16:
        let quant = quantity8x10[cellRow]
        quantity8x10[cellRow] = quant+1
        break;
    default:
        break
    }
}

func minusButtonClick(tag:Int,cellRow:Int)
{
    switch tag
    {
    case 17:
        let quant = quantity4x4[cellRow]
        quantity4x4[cellRow] = quant-1
        break;
    case 18:
        let quant = quantity4x6[cellRow]
        quantity4x6[cellRow] = quant-1
        break;
    case 19:
        let quant = quantity5x7[cellRow]
        quantity5x7[cellRow] = quant-1
        break;
    case 20:
        let quant = quantity8x10[cellRow]
        quantity8x10[cellRow] = quant-1
        break;
    default:
        break
    }
}

I gave different tags to all buttons. when I run the application, it gives me the following error: "Could not assign a value of type UITableViewWrapperView to UITableView" in the line where I set my table view.

0
source share
2 answers

sender.superview!.superview?.superview as! UITableView . iOS6 iOS7 .

, rowIndex , cellForRowAtIndexPath. :

class SizeAndQuantityCellView:UITableViewCell
{
    var rowIndex: Int = 0
    ...
}

TableViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "myTableViewCell", for: indexPath) as! SizeAndQuantityCellView
   cell.rowIndex = indexPath.row
   ...
   return cell
}

, , , quantity4x4 [cellRow], , . I.o.w. SizeAndQuantityCellView ViewController SizeAndQuantityCellView. , . , ViewController .

0

. , , UIButton, IndexPath ( ) .

, UIButton IndexPath, .

, :

extension UIButton {
    func packing(low:Int, high:Int) -> Int {
        //With the packing function we force our Packed number to be a 64 bit one
        //we shift the high part 32bits to the left and OR the resulting value with the low part
        return ((high << 32) | low)
    }
    func unpackHigh(packed:Int) -> Int {
        //Unpacking the high part involve swifting to right the 
        //number in order to zero'ing all the non relevant bits.
        return packed >> 32
    }
    func unpackLow(packed:Int) -> Int {
        //Unpacking the low part involve masking the whole packed number with the max value allowed for an Int.
        //note that using the Int.max function does not work as expected, returning a compile error. 
        //Maybe, it a bug of compiler.
        let mask = 2147483647
        return mask & packed
    }

    //since we cannot use stored property on extensions, we need to compute realtime, every time the
    //right value of our indexPath.
    var indexPath:IndexPath {
        get {
            return IndexPath(row: unpackLow(packed: self.tag), section: unpackHigh(packed: self.tag))
        }
        set {
            self.tag = packing(low: newValue.row, high: newValue.section)
        }
    }
}

cellForRowAtIndexPath:

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

let aCell = tableView.dequeueReusableCell(withIdentifier: "reuseCell") as! CustomTableViewCell

...

aCell.aButton.indexPath = indexPath

...

return aCell

}

, indexPath , .

0

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


All Articles