0

I am trying to use a UITableview with multiple selection on and a check mark accessory view for selected rows. This is mostly working if I turn on and off the accessory view in tableView:didSelectRow.

However, I tried to build a selectAll method, and I found that the array of selected cells was being cleared after I had spun through all the cells and selected them if I then call reloadData().

I suspect reloading the table clears selection. I don't know of any other way to have all the cells drawn after I set the selected flag and accessory view.

I am wondering if I need to keep my own array of selected rows. Has anyone else built something like this? Its seems like a common scenario.

Any tips or sample code appreciated.

Bista
  • 7,869
  • 3
  • 27
  • 55

4 Answers4

0

Take an Array and add the indexPath of each selected cell into it and put a condition in cellForRowAt... that if the Array contains that particular indexPath, set it as selected.

Bista
  • 7,869
  • 3
  • 27
  • 55
0

you need add some functionality in cellForRowAtIndexPath method like this ang your view controller code like this

let we take one example of photo gallery application

class CreateEvent: UIViewController,UITableViewDataSource,UITableViewDelegate {

var yourArray :  [Photo] = [Photo]()
//MARK: - Content TableView Methods
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
    let objPhoto = yourArray[indexPath.row]
    if objPhoto.isPhotoSelected == true
    {
        cell.accessoryType = .Checkmark
    }

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let objPhoto = yourArray[indexPath.row]
    objPhoto.isPhotoSelected = true
    let cell = tableView.cellForRowAtIndexPath(indexPath)
    cell.accessoryType = .Checkmark

}
//MARK: Action Method

func selectAllPhoto()
{
    for objPhoto in yourArray
    {
        objPhoto.isPhotoSelected = true
    }

    tableView.reloadData()
}
}

and one more thing you need to create your custom object like

class Photo: NSObject {

var photoName:String = ""
var isPhotoSelected  = false

}

hope this will help you

Dhaval Bhadania
  • 3,090
  • 1
  • 20
  • 35
jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
0

There are two approaches you can take. One is to track the selected row numbers. To do this, you can use an NSMutableIndexSet or its Swift counterpart IndexSet.

Essentially, when a row is selected, add it to the set. When you deselect it, remove it from the set. In cellForRowAtIndexPath you can use containsIndex to determine if a check mark should be shown or not.

Since you explicitly mention an issue with selection when you reload the table, it is worth considering the issue with storing row numbers (whether in a set or an array), and that is that row numbers can change.

Say I have selected rows 4,7 and 9 and these values are stored in the index set. When I reload the data, new data may have been inserted after the "old" row 8, so now I should be selecting rows 4,7 and 10, but I will be selecting 4,7 and 9 still.

A solution to this is to store some sort of unique identifier for the data that should be selected. This will depend on your data, but say you have a string that is unique for each item. You can store this string in a NSMutableSet or Swift Set, which again makes it easy to check if a given item is selected using contains

Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

The best approach for multiple selection is

Take a model object, in that take all your attributes and one extra boolean attribute (like isSelected) to hold the selection.

In case of selecting a row

  1. Fetch the relevant object from the array
  2. Then update the isSelected boolean (like isSelected = !isSelected) and reload table.

In case of select all case

  1. Just loop through the array.
  2. Fetch the model object from array.
  3. make the isSelected = true.
  4. After completion of loop, reload the table.
halfer
  • 19,824
  • 17
  • 99
  • 186
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43