瞄准一个单元格 - 删除线以将任务项目标记为完成 - Swift 3

时间:2017-01-09 03:11:06

标签: ios uitableview swift3

我是Swift和XCode的新手。我正在创建待办事项列表应用程序。当用户在表格中滑动单元格并单击“完成”按钮时,我尝试将任务标记为完成,但是我难以针对特定的表格单元格来执行此操作。我找到了这个例子:UILabel with text struck through(参见MicroR的评论),这帮助我实际弄清楚如何对给定的文本做一个删除线,如果我只是添加它,我就有了删除线的基本概念当我在cellForRowAtIndexPath函数中创建表格单元格时。问题在于,当用户在刷新表格中的特定单元格后单击完整按钮时,我只想将任务标记为完成。在用户按下“完成”按钮后,我需要一些用于定位单元格的代码的帮助(此函数是下面代码中的最后一个,但为了清楚起见,我在此函数中包含了其余的代码)。谢谢!

当我将它放入我的cellForRowatIndexPath函数时,这是允许删除线发生的代码

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))

这是我的代码:

// I pull in fake data right now from another function - sentData1 contains my task item.
var sentData1:String!
var sentData2:String!
var sentData3:String!
var sentData4:String!
var sentData5:String!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    self.navigationItem.title = sentData5
}

  override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.section [section]
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return self.section.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Task", for: indexPath)

    cell.selectionStyle = .none

    switch (indexPath.section)
    {
    case 0:
        cell.textLabel?.text = sentData1
    case 1:
        cell.textLabel?.text = sentData2
    case 2:
        cell.textLabel?.text = sentData3
    case 3:
        cell.textLabel?.text = sentData4
    default:
        cell.textLabel?.text = "Other"

    }
    return cell     
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
        print("delete button tapped")
    }
    delete.backgroundColor = UIColor(red: 27/255, green: 124/255, blue: 150/255, alpha: 1.0)

    let edit = UITableViewRowAction(style: .normal, title: "Edit") { action, index in
        print("edit button tapped")
    }
    edit.backgroundColor = UIColor(red: 130/255, green: 208/255, blue: 216/255, alpha: 1.0)

    let markComplete = UITableViewRowAction(style: .normal, title: "Complete") { action, index in
        // this is where I want to target the cell!
        print("complete button tapped")

    }
    markComplete.backgroundColor = UIColor(red: 0/255, green: 66/255, blue: 89/255, alpha: 1.0)
    return [edit, delete, markComplete]
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
{
    return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

}

1 个答案:

答案 0 :(得分:0)

:) 我也是Swift和XCode的新手.. 我的英语也不好:) 但如果我了解你
我认为你的问题的答案是......:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
    {   
        let delete = UITableViewRowAction(style: .normal, title: "Delete") { action, index in
            self.Apertou_Botao(pIndice: indexPath.row, pTexto: "Apertou Deletar")
        }
        delete.backgroundColor = UIColor(red: 27/255, green: 124/255, blue: 150/255, alpha: 1.0)
 return [delete]
}

func Apertou_Botao(pIndice : Int , pTexto : String)
    {
        print("\(pTexto) indice Clicado..: \(pIndice)");
    }
}