更新整个tableView的uiTableViewCell上的按钮

更新整个tableView的uiTableViewCell上的按钮,uitableview,swift,Uitableview,Swift,我有一个类别BrandOfCarTableViewCell:UITableViewCell,它有标签和按钮。 一个按钮增加标签上的数字,第二个按钮减少标签上的数字。 此外,我还有第二个类类型的TransportTableViewController:UITableViewController,它具有tableView的所有操作 我需要给tableViewController一个单击按钮的indexOf单元格。我还需要传递标签数量的值 如何使用Swift语言实现这一点?代码的基本结构应如下所示 p

我有一个类别BrandOfCarTableViewCell:UITableViewCell,它有标签和按钮。 一个按钮增加标签上的数字,第二个按钮减少标签上的数字。 此外,我还有第二个类类型的TransportTableViewController:UITableViewController,它具有tableView的所有操作

我需要给tableViewController一个单击按钮的indexOf单元格。我还需要传递标签数量的值


如何使用Swift语言实现这一点?

代码的基本结构应如下所示

protocol BrandOfCarTableViewCellDelegate {
 func updateLabelCountWithValue:(cellIndex: Int ,newValue :Int)
}

class BrandOfCarTableViewCell: UITableViewCell  {
 //This class can have your label and the two buttons
 var cellIndex: Int = 0
 var delegate: BrandOfCarTableViewCellDelegate?

 func setUpCellWithIndex:(index: Int)
  self.cellIndex = index
 }

 @IBAction func buttonTapped() {
 //Call the delegate
 }
}

class TypeOfTransportTableViewController: UITableViewController, BrandOfCarTableViewCellDelegate {

 func updateLabelCountWithValue:(cellIndex: Int ,newValue :Int) {
 }
}

非常感谢库马尔

 protocol BrandOfCarTableViewCellDelegate{
func updateLabelWithValueAtIndex(cellIndex:Int, newValue: Int)
}
class BrandOfCarTableViewCell: UITableViewCell {

var delegate:BrandOfCarTableViewCellDelegate?
@IBAction func btnPlus(sender: AnyObject) {

    cellIndex = btnPlusOutlet.tag

    counter = counter + 1

    // call the delegate
    delegate?.updateLabelWithValueAtIndex(cellIndex!, newValue: counter)
}


@IBAction func btnMinus() {

    cellIndex = btnPlusOutlet.tag

    if counter >= 1 {
         counter = counter - 1
    }
    else{ return }

    // call the delegate
    delegate?.updateLabelWithValueAtIndex(cellIndex!, newValue: counter)
}
}

class TypeOfTransportTableViewController: UITableViewController {

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("BrandOfCarCell",     forIndexPath: indexPath) as BrandOfCarTableViewCell

    cell.delegate = self
    cell.btnPlusOutlet.tag = indexPath.row
    cell.labelQty.text = String(arrayBrandsOfTransport[indexPath.row].qty)
}


func updateLabelWithValueAtIndex(cellIndex:Int, newValue: Int){
//        println("table view controller cell index: \(cellIndex)" )
//        println("the newvalue is \(newValue)")
        arrayBrandsOfTransport[cellIndex].qty = newValue
        tableView.reloadData()
    }
}

我刚刚更新了数组的值,我保留了所有的值并重新加载了表!谢谢库马尔

你好,库马尔,谢谢你的回答。1我应该如何使用索引功能校准SetupCell?我使用cell.cellIndex=indexPath.row 2在cellForRowAtIndexPath的TypeOfTransportTableViewController中设置了索引。我如何调用委托?是这样吗。updateLabelCountWithValuecellIndex:Int,newValue:IntI本可以编写整个代码,但我想让您找出剩下的部分。更新按钮的标签可能不是最佳做法。而是调用一个方法单元格。setUpCellWithIndex具有所需的索引。现在您将面临另一个问题,因为该函数不是公共函数。现在我想让你明白: