在swift 3中的tableView中按下了哪个单元格按钮

时间:2017-04-24 12:44:27

标签: ios uitableview swift3 uibutton cell

我已经阅读过类似的问题,但这些代码对我没有用,所以请帮忙: 我有一个表格视图,每个单元格中都有一些按钮 - 这个表格视图是用户的因素,我在该表格视图中添加了按钮 -​​ 当用户付钱时,该按钮隐藏在该单元格中的该因子中,而另一个单元格中的按钮被隐藏用户没有支付工资是不隐藏的 - 这就是我做的 我想检测哪些按钮按下了?! 我知道在表格视图中我们可以检测到哪个单元格已被按下但是这是不同的,因为用户只需按下该单元格中的按钮不按下所有单元格

由于我的代码太长,我只需将表格视图代码放在这里

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "factorCell", for: indexPath) as! FactorCell

    cell.factorTitle.text = factorTitle[indexPath.row]
    cell.factorCode.text = factorCodes[indexPath.row]
    cell.factorDate.text = factorDate[indexPath.row]
    cell.factorHour.text = factorHour[indexPath.row]
    cell.factorPrice.text = factorPrice[indexPath.row]
    cell.factorCondition.text = factorConditions[indexPath.row]
    cell.factorBill.tag = indexPath.row
 cell.factorBill.addTarget(self,action:#selector(factorViewController.Bill), for: UIControlEvents.touchUpInside)

    cell.factorDetail.tag = indexPath.row    

    cell.factorDetail.addTarget(self,action:#selector(factorViewController.FactorDetail), for: .touchUpInside)


    if   factorConditions[indexPath.row] == "پرداخت شده"  {


        cell.factorBill.isHidden = true


    } else {

        cell.factorCondition.textColor = UIColor.red

        cell.factorBill.isHidden = false

    }


    return cell

}

这是我想要的功能: 当用户点击支付他的因子并按下按钮 - 移动到另一个视图控制器并在该视图控制器中,用户可以在该视图控制器中看到这些数据 -

**非常感谢如果你不明白我想做什么告诉我,我会解释更多**

4 个答案:

答案 0 :(得分:7)

在自定义单元格类中为您创建IBAction按钮,然后声明将在按钮单击时调用的块。

var ButtonHandler:(()-> Void)!

    @IBAction func ButtonClicked(_ sender: Any) {
            self.ButtonHandler()
        }

然后在用于行方法的单元格内的tableview类中,

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = playlistTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell
        cell.ButtonHandler = {()-> Void in
            // your code 
        }
        return cell
}

答案 1 :(得分:2)

您在cellForRowAt中设置了因子按钮标记,因此您可以轻松地从factorViewController.Bill获取索引路径,并从该索引路径中获取整个单元格,您可以执行任何操作需要。

你可以获得类似的细胞,

 let cell = tableView.cellForRowAtIndexPath(indexPath)

您可以参考this so post了解如何从按钮点击获取索引路径!

答案 2 :(得分:1)

当您按下#include <QtScript> #include <QtConcurrent> #define THREADS 50 void worker_function(const QString &function) { QScriptEngine engine; QScriptValue value; value = engine.evaluate(function); value.call(); } ... QFutureSynchronizer<void> synchronizer; //Set the max pooled threads QThreadPool::globalInstance()->setMaxThreadCount(THREADS); //Start all threads and add them to the future synchronizer for (int i = 0; i < THREADS; i++) synchronizer.addFuture(QtConcurrent::run(worker_function, QString("(function() {var r = Math.random(); while(1>0) print(r);})"))); //Wait for all threads to finish synchronizer.waitForFinished(); ... 按钮时,您的Bill方法将会调用。因此,您可以通过factorBill轻松区分Bill方法中的按钮。您已在sender.tag中设置了标记。 / p>

答案 3 :(得分:1)

  Here is update

just leave it, i explained it more simple,
create a button action in your cell class,then
Writre the code:-

> //Cell class

protocol CellDelegate: class {
    func didTapCell(index: IndexPath)
}
  var delegateCell:CellDelegate?
  var indexPath:IndexPath?
 @IBAction func buttonCellClick(_ sender: Any) {
        delegateCell?.didTapCell(index: indexPath!)
    }

> //In ViewController

 cell.delegateCell = self
 cell.indexPath = indexPath

 func didTapCell(index: IndexPath) {
        print("PRESS BUTTON >> \(index.row)")
    }