Ankit0047 Ankit0047 - 21 days ago 5
Swift Question

Swift: I want to know what is the index path row of the button that i clicked?

I have a custom cell class given 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 click the plus button the quantity should increase by one and when i click the minus button it should decrease by one.
Here's my controller class for that:

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 have given different tags to all the buttons.
when i run the app it gives me the following error: "Could not cast value of type UITableViewWrapperView to UITableView" at the line where i set my tableview.

Answer

Doing sender.superview!.superview?.superview as! UITableView is very dangerous. In the transition between iOS6 and iOS7, an extra layer was actually introduced and that kind of call failed.

Rather just have a property rowIndex in cell, which you set in your cellForRowAtIndexPath. For Example:

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

In your 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
}

From your code, it is not clear where quantity4x4[cellRow], for example, fits in but it seems to me that a Delegation Pattern might also be handy. I.o.w. Create a delegate protocol for SizeAndQuantityCellView and let your ViewController be the delegate of SizeAndQuantityCellView. When the buttons is tapped, fire an event to the delegate. That way your ViewController can handle the logic upon the pressing of the buttons.

Comments