samedi 27 juin 2015

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.

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 (selectedMarks.contains(currentStat.statEvents[indexPath.row])){
        selectedMarks.removeAtIndex(indexPath.row)
    } else {
        selectedMarks.append(currentStat.statEvents[indexPath.row])
    }
    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

Somebody knows how do I solve this? Probably there's another way to get the selected rows array info...

Aucun commentaire:

Enregistrer un commentaire