Swift:我想知道我点击的按钮的索引路径行是什么?

时间:2016-11-14 07:34:05

标签: ios swift xcode

我有一个自定义单元格类,如下所示:

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!)
}
}

我想要做的是当我点击加号按钮时,数量应该增加1,当我点击减号按钮时,它应减少1。 这是我的控制器类:

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的那一行。

2 个答案:

答案 0 :(得分:0)

执行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可以在按下按钮时处理逻辑。

答案 1 :(得分:0)

更软化的方法,涉及使用扩展和按位运算符。简化,您可以使用内置于每个UIButton的tag属性,通过使用按位运算符和移位打包来存储of和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's 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传递给单元格,以便触发扩展方法。