swift 3原型(prototype)单元格当前行按钮

标签 swift uitableview

我尝试做一个待办事项应用程序。 我使用原型(prototype)电池,目前遇到一个问题,当我按下按钮时,没有任何反应

到目前为止,这可以获取按钮的行。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell

    let task = tasks[indexPath.row]


    cell.uncheckbox.tag = indexPath.row
    cell.uncheckbox.addTarget(self, action: #selector(ViewController.Test), for: UIControlEvents.touchUpInside)

    cell.textField.isEnabled = false
    cell.importantImage.isHidden = true


    if(task.isImportant){
        cell.textField?.text =  task.name!
        cell.importantImage.isHidden = false;
    }else{
        cell.textField?.text = task.name!
    }

    return cell;
}

@IBAction func Test(sender: UIButton)
{
    let row = sender.tag

    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell") as! TableViewCell

    print(row)
    print(row == 0)
    if(row == 0){

         cell.textField?.text =  "change the text"
    }



}

问题是,一旦我按下按钮,当前单元格就不会发生任何变化。我猜它还没有设置到正确的行中。

当我只打印行然后按下按钮时,我从按钮获得正确的值

最佳答案

找到了一种方法:

我在 TableViewCell 类中调用了按钮

 // when i press the add button it checks all buttons
@IBAction func testing(_ sender: Any) {
    if(isBoxClicked == true)
    {
        uncheckbox.setImage(checkBox, for: UIControlState.normal)
        self.textField.backgroundColor = UIColor.red
        isBoxClicked = false
    }else{
       uncheckbox.setImage(uncheckBox, for: UIControlState.normal)
         self.textField.backgroundColor = UIColor.white
        isBoxClicked = true
    }
} 

在 vieClass 中我使用了这个

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell 

cell.testing(cell.uncheckbox)

  return cell;
}

关于swift 3原型(prototype)单元格当前行按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41913856/

相关文章:

ios - TableView 不在 Swift 中注册点击,自定义单元格不显示

ios - 如何在 iOS 项目中正确配置 Realm DB 文件 (<db_name>.realm) 的路径?

swift - 尝试在 Swift 中使用 PartialKeyPath 修改类属性

ios - UITableView 标题不听滚动

iphone - 如何向上移动 tableView 以便从底部弹出一个 View ,使其不在 iPhone 的表格顶部

ios - Swift - 刷新 UITableView 但保持表中的当前位置

ios - 隐藏选项卡时如何拉伸(stretch) View

ios - 父 View 中容器 View 的自动布局未与布局指南对齐

ios - MFMailComposeViewController 不会关闭

ios - 从静态方法更新 UI TableView