Ruben
Ruben

Reputation: 1859

Get multiple selected rows info of an uitableview

I have so far a tableView that works with multiple selection rows. Everything is working fine except when I'm trying to get an array of the rows that I've selected.

This is my Stat.swift class:

class Stat: Equatable {
      var statName: String = ""
      var statCalendar: String = ""
      var statEvents : [StatEvents] = []
}

struct StatEvents {
    var isSelected: Bool = false
    var name: String
    var dateRanges: [String]
    var hours: Int
}
func == (lhs: Stat, rhs: Stat) -> Bool {
     return (lhs.statEvents == rhs.statEvents)
}

And here is my EventsViewController.swift class:

var currentStat = Stat()
var selectedMarks = [StatEvents]()

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

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

    cell.textLabel?.font = UIFont.systemFontOfSize(8.0)
    cell.textLabel?.text = "\(currentStat.statEvents[indexPath.row].name)  \(currentStat.statEvents[indexPath.row].dateRanges) horas=\(currentStat.statEvents[indexPath.row].hours)"

    if currentStat.statEvents[indexPath.row].isSelected{
        cell.accessoryType = .Checkmark

    } else {
        cell.accessoryType = .None
    }
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    currentStat.statEvents[indexPath.row].isSelected = !currentStat.statEvents[indexPath.row].isSelected

    if (contains(selectedMarks, currentStat.statEvents[indexPath.row])) {
        //To-Do: remove the object in selectedMarks
    } else {
        selectedMarks.append(currentStat.statEvents[indexPath.row]) //add the object to selectedMarks
    }

    tableView.reloadData()
}

The problem is in the "didSelectRowAtIndexPath" method. When I select any row, it appends the object in the "selectedMarks" array (this is working fine), but the problem is when I deselect some of those rows, it should erase back the object in the selectedMarks array. I'm trying to use the "contains" method, but I get a compilation error in that line

could not find an overload for contains that accepts the supplied arguments

I updated my question by adding the Equatable protocol in the Stat class, but I get the same error again:

could not find an overload for contains that accepts the supplied arguments

and also getting a new error:

Command failed due to signal: Segmentation fault: 11

Upvotes: 0

Views: 854

Answers (1)

Cihan Tek
Cihan Tek

Reputation: 5409

In order for the contains method to do its job in Swift 2, your StatEvents struct should conform to the Equatable protocol as in the example below:

struct StatEvents: Equatable
{
    // ...  
    // implementation of your structure....
    // ...
}    

// Needed for conforming to the Equatable protocol

func == (lhs: StatEvents, rhs: StatEvents) -> Bool
{
    // Return true if the parameters are equal to each other
}

Also, there is no global contains function in Swift 2, so you need to call the new array extension method contains instead, which in your case would be like this:

selectedMarks.contains(currentStat.statEvents[indexPath.row])

Also add the protocol declaration to the StatEvents structure, not to the Stat class. Your implementation of the == method is also incorrect. It should check for the equality between two object of the StatEvents type as I have shown above.

Upvotes: 2

Related Questions