我有一個自定義單元格類如下:Swift:我想知道什麼是我點擊的按鈕的索引路徑行?
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!)
}
}
我想要做的就是當我點擊加號按鈕的數量應當由一個增加,當我點擊減號按鈕應該減一。 這是我爲控制器類:
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
}
}
我已經給了不同的標籤,所有的按鈕。 當我運行應用程序時,它給了我以下錯誤:「無法將類型UITableViewWrapperView的值轉換爲UITableView的值」在我設置我的tableview的行。
做'sender.superview!.superview?.superview'是一個壞主意,如果蘋果決定在未來的更新中更改它們的實現,您的代碼將會中斷。你應該有一個顯式變量,用於從storyboard鏈接的tableview或 – Fonix
將cellForRow中按鈕的標記設置爲indexPath。row –
我有顯式tableView變量鏈接storyboard,但它在ViewController類,但我想訪問在上面給出的定製UITableViewCell類中的tableView – Ankit0047