1

Issue 1: Check Marks Keep Disappearing when scrolling.

Issue 2: Need help adding/removing from array with unique ID to prevent duplicates.

I am trying to insert/remove a cellTextLabel from an empty array. I can't seem to find a good solution. Here's what I've tried and why it failed.

Bad Option 1

// Error = Out of range (I understand why)
myArray.insert(cell.textLabel.text, atIndex: indexPath)

Bad Option 2

// I won't have a way to reference the array item afterwards when I need to remove it. Also, this option allows for the same string to be entered into the array multiple times, which is not good for me.
myArray.insert(cell.textLabel.text, atIndex: 0)

Below is the code so far, any help is greatly appreciated. Thank you.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let row = indexPath.row
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("items", forIndexPath: indexPath) as UITableViewCell

    var myRowKey = myArray[row]
    cell.textLabel.text = myRowKey

    cell.accessoryType = UITableViewCellAccessoryType.None
    cell.selectionStyle = UITableViewCellSelectionStyle.None


    return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {


    let selectedCell  = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

    if selectedCell.accessoryType == UITableViewCellAccessoryType.None {

    selectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
    }

    var selectedItem = selectedCell.textLabel.text!

    println(selectedItem)

}

func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {

    let deSelectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

    if deSelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark {
        deSelectedCell.accessoryType = UITableViewCellAccessoryType.None
    }

    var deSelectedItem = deSelectedCell.textLabel.text!

    println(deSelectedItem)

}
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
cheelo007
  • 51
  • 12

1 Answers1

0

Issue 1: Your checkmarks keep disappearing when you're scrolling because of the following line in the didDeselectRowAtIndexPath method:

let deSelectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

The call to cellForRowAtIndexPath will create a NEW cell. It will NOT modify the currently visible cell on the screen in your UITableView. This is because cells are reused as items scroll on and off the screen, with new data loaded into them.

To retain the selection status of your cells, you will need to upgrade your data model a bit. Right now your data comes from the myArray which is a String array. You could try something as follows instead:

struct Item {
  var name: String // The string value you are putting in your cell
  var isSelected: Bool // The selected status of the cell
}

Then you would define your data something like this:

var myArray = [
    Item(name: "Cell 1 value", isSelected: false),
    Item(name: "Cell 2 value", isSelected: false),
    ...
  ]

And your tableView:didSelectRowAtIndexPath method would look more like this:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     // Toggle the selected state of this cell (e.g. if it was selected, it will be deselected)
    items[indexPath.row].isSelected = !items[indexPath.row].isSelected

     // Tell the table to reload the cells that have changed
    tableView.beginUpdates()
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
    tableView.endUpdates()

    // Reloading that cell calls tableView:numberOfRowsInSection and refreshes that row within the tableView using the altered data model (myArray array)
  }

Next time you tap that row, the tableView:didSelectRowAtIndexPath method will fire again and toggle the selected state of that cell. Tell that cell to reload will refresh the cell that is actually visible on the screen.

Issue 2: Without knowing too much about the type of data you want to keep unique and how you are adding/removing in ways that could add duplicates, you might want to take a look at this answer for removing duplicate elements from your array. One way is to use a set, which will not preserve order but will ensure elements only occur once.

Community
  • 1
  • 1
Undrea
  • 494
  • 5
  • 7