0

I have an array called teamArray which contain a teamObject with a id, name and shortname. these names is looped into and populated in an tableView. All these cells can be selected and when a cell is selected the indexPath is inserted into a cellSelected array. I would like to save the selected cells id's from the teamArray into a new array. So I guess I need to compare the cellSelected arrays indexPath with the teamArray? How can I do this?

arrays

var teamArray = Array<Team>()
var cellSelected = NSMutableArray()

didSelectRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)

    if (self.cellSelected.containsObject(indexPath)) {
        self.cellSelected.removeObject(indexPath)
    } else {
        self.cellSelected.addObject(indexPath)


    }

    tableView.reloadData()
}

teamObject

class Team: NSObject{
    var id: Int!
    var name: NSString!
    var shortname: NSString!

    init(id: Int, name:NSString, shortname: NSString) {
        self.id = id
        self.name = name
        self.shortname = shortname
    }
}
4
  • So what problem are you facing? Commented Apr 30, 2015 at 9:59
  • 1
    What do you mean by "cells id's"? Commented Apr 30, 2015 at 9:59
  • not able to understand your problem. Commented Apr 30, 2015 at 10:02
  • I've added my teamObject so you can see. The id's is the id in the database, which i need for some Api calls. Commented Apr 30, 2015 at 10:08

2 Answers 2

2

var teamSelected = [Team]()

In your didSelectRowAtIndexPath function:

let team = self.teamArray[indexPath.row] as Team
var removed = false

for (index, value) in enumerate(self.teamSelected) {
    if (value == team) {
        self.teamSelected.removeAtIndex(index)
        removed = true
    }
}

if (!removed) {
    self.teamSelected.append(team)
}

in your cellForRowAtIndexPath function:

let team = self.teamArray[indexPath.row] as Team
var removed = false

for (index, value) in enumerate(self.teamSelected) {
    if (value == team) {    
        cell.accessoryView = cell.accessoryCheck
        removed = true
    }
}

if (!removed) {
    cell.accessoryView = cell.accessoryUncheck
}

You can then loop through teamSelected when you want to access the ID of the selected Team's

EDIT: Changed name of cellSelected to teamSelected

Sign up to request clarification or add additional context in comments.

5 Comments

Team does not have a member named containsObject
i still get [(Team)] does not have a member named containsObject
Okay but now when i press a already selected row it does not remove the selected ? like removeObject
Updated. I don't have my Mac with me so I'm unable to test any of it.
Seem to work now. Have made some changes syntax whise and added cellForRowAtindexPath. Please check :)
0

Your main teamArray, which is load in TableView

var teamArray = ["Kirit Modi","Kirit Modi 1","Kirit Modi 2","Kirit Modi 3","Kirit Modi 4","Kirit Modi 5","Kirit Modi 6","Kirit Modi 7","Kirit Modi 8","Kirit Modi 9"]

And cellSelected,

var cellSelected = : [String] = []

In TableView delegate didSelectRowAtIndexPath

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

        if contains(cellSelected, Name[indexPath.row])
        {
            cellSelected.removeObject(Name[indexPath.row])
        }
        else{
            cellSelected.append(Name[indexPath.row])
        }
    }

Use the below function to remove object from Array.

extension Array {
    mutating func removeObject<U: Equatable>(object: U) {
        var index: Int?
        for (idx, objectToCompare) in enumerate(self) {
            if let to = objectToCompare as? U {
                if object == to {
                    index = idx
                }
            }
        }

        if(index != nil) {
            self.removeAtIndex(index!)
        }
    }
}

2 Comments

You can simplify your remove function slightly as mutating func removeValue<U: Equatable>(object: U) { for (idx, objectToCompare) in enumerate(self) { if (objectToCompare as? U) == object { removeAtIndex(idx); return } } }
Or avoid the casting if you write it as a free function: func removeValue<C: RangeReplaceableCollectionType where C.Generator.Element: Equatable>(inout source: C, value: C.Generator.Element) { if let idx = find(source, value) { source.removeAtIndex(idx) } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.