2017-01-09 22 views
0

我是Swift和XCode的總新手。我正在創建一個待辦事項列表應用程序。當用戶滑動表格中的單元格並單擊「完成」按鈕時,我試圖將任務標記爲完成,但爲了執行此操作,我無法定位特定的表格單元格。我找到了這個例子:UILabel with text struck through(參見MicroR的評論),它幫助我真正弄清楚如何在給定的文本上做一個刪除線,並且我有刪除線工作的基本思想,如果我在創建我的時候添加它我的cellForRowAtIndexPath函數中的表格單元格。問題是我想只在用戶在表格中的特定單元格上滑動後單擊完整按鈕時纔將任務標記爲完成。在用戶按下「完成」按鈕(這個函數是下面代碼中的最後一個,但爲了清楚起見,我將其餘代碼包含在此函數中)後,我需要一些關於定位單元格的代碼。謝謝!目標單元格 - 刪除線將任務項目標記爲完成 - Swift 3

這裏是允許的刪除時,我把它放在我的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) { 

} 
+0

我想你應該管理lastSelectedIndexPath變量 –

+0

我不知道你的意思 - 我沒有看到文檔。你能詳細說明一下嗎? – r4clark

回答

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)"); 
    } 
}