Swift 3.0如何使用TableView单元格更改按钮标题

时间:2017-08-03 14:41:29

标签: ios swift uitableview button

我有一个包含子视图表视图单元格的表视图。我已经附加了一个按钮,并将其连接到表格视图单元格的VC。我希望按钮在加载时说“添加”,单击时“减去”,再次单击时返回“添加”。但我无法理解如何将行号与按钮状态联系起来。

带表格视图的VC:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TVCell
    cell.cellDelegate = self

    if (searchActive) {
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = data[indexPath.row]
    }

    return cell
}

func didPressButton(cell: TVCell) {

    guard let indexPath = self.tableView.indexPath(for: cell) else {
                   return
    }

    print("Button tapped on row \(indexPath.row)")
}

用于表格查看单元格的VC:

protocol TVCellDelegate : class {
    func didPressButton(cell: TVCell)
}

class TVCell: UITableViewCell {

    @IBOutlet weak var addButton: UIButton!

    weak var cellDelegate: TVCellDelegate?

    override func prepareForReuse() {
        super.prepareForReuse()
        self.cellDelegate = nil
    }

    // connect the button from your cell with this method
    @IBAction func buttonPressed(sender: UIButton) {
        self.cellDelegate?.didPressButton(cell: self)
    }
}

2 个答案:

答案 0 :(得分:1)

存储按钮'在视图控制器中显示状态,并在重复使用和按下按钮后更改文本。

VC with TableView:

var isSubtracting = [IndexPath: Bool]()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TVCell
    cell.cellDelegate = self

    if isSubtracting[indexPath] ?? false {
         cell.addButton.setTitle("Add", for: .normal)
    } else {
         cell.addButton.setTitle("Subtract", for: .normal)
    }

    cell.indexPath = indexPath

    if(searchActive) {
        cell.textLabel?.text = filtered[indexPath.row]
    } else {
        cell.textLabel?.text = data[indexPath.row]
    }

    return cell
}

func didPressButton(indexPath: IndexPath) {
   guard let cell = tableView.cellForRow(at: indexPath) as? TVCell else {
        return
    }

   if isSubtracting[indexPath] ?? false {
         isSubtracting[indexPath] = false
         cell.addButton.setTitle("Subtract", for: .normal)
    } else {
         isSubtracting[indexPath] = true
         cell.addButton.setTitle("Add", for: .normal)
    }
}

用于表格查看单元格的VC:

protocol TVCellDelegate : class {
    func didPressButton(indexPath: IndexPath)
}

class TVCell: UITableViewCell {
    @IBOutlet weak var addButton: UIButton!

    weak var cellDelegate: TVCellDelegate?

    var indexPath: IndexPath!

    override func prepareForReuse() {
        super.prepareForReuse()
        self.cellDelegate = nil
    }

    // connect the button from your cell with this method
        @IBAction func buttonPressed(sender: UIButton) {
        self.cellDelegate?.didPressButton(indexPath: indexPath)
    }
}

答案 1 :(得分:0)

您可以为此类按钮的状态添加标题

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TVCell
        cell.cellDelegate = self
        cell.indexPath = indexPath

        cell.addButton.setTitle("Add",for: .normal)
        cell.addButton.setTitle("Subtract",for: .selected)

        if(searchActive) {
            cell.textLabel?.text = filtered[indexPath.row]
        } else {
            cell.textLabel?.text = data[indexPath.row]
        }

        return cell
    }

和tableViewCell

protocol TVCellDelegate : class {
    func didPressButton(indexPath: IndexPath)
}

class TVCell: UITableViewCell {
    @IBOutlet weak var addButton: UIButton!

    weak var cellDelegate: TVCellDelegate?

    var indexPath: IndexPath!

    override func prepareForReuse() {
        super.prepareForReuse()
        self.cellDelegate = nil
    }

    // connect the button from your cell with this method
    @IBAction func buttonPressed(sender: UIButton) {
        if(sender.isSelected){
            sender.isSelected = false
        }else{
            sender.isSelected = true
        }
        self.cellDelegate?.didPressButton(indexPath: indexPath)
    }
}