I am a total newbie to Swift and XCode. I am in the process of creating a to do list application. I am attempting to mark a task as complete when a user swipes the cell in the table and clicks a "complete" button, but I am having difficulty targeting a specific table cell in order to do this. I found this example: UILabel with text struck through (see comment by MicroR), which helped me actually figure out how to do a strikethrough on a given piece of text, and I have this basic idea of strikethrough working if I just add it in when I create my table cells in my cellForRowAtIndexPath function. The issue is that I would like to only mark a task as complete when a user clicks the complete button after swiping on that specific cell in the table. I need some help with the code for targeting a cell after the user pushes the "complete" button (this function is the last one in the code below, but I included the rest of the code in this function for clarity). Thank you!
Here is the code that allows a strikethrough to happen when I put it in my cellForRowatIndexPath function
let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: message)
attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length))
Here is my code:
// 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) {
}