0

All, Can you give me a source code of my below problem?

I have 100 user array and load that array value(username) in tableview.

Scenario:

When I click any tableviewcell, i store that value in new array( userArray) and also i have displayed right side discloser (checkmark). But my problem is when i click again to deselect that user from tablecell my app has been crashed due to index out of bound.

I know problem is out of index, but how can i resolve that issue so if i click again on that tablecell it will remove user from userArray and i get new fresh userArray?

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

let tagValue = 100 + indexPath.row
let cell: UITableViewCell = tableView.viewWithTag(tagValue) as UITableViewCell

if cell.accessoryType == UITableViewCellAccessoryType.None
{
    cell.accessoryType = UITableViewCellAccessoryType.Checkmark 
    userToSendPost.append(self.users2[indexPath.row])
} else {
    cell.accessoryType = UITableViewCellAccessoryType.None
    let tempString = userToSendPost[indexPath.row] //tagvalue
    let objcArray = userToSendPost as NSArray
    let indexOfObject = objcArray.indexOfObject(tempString)
    userToSendPost.removeAtIndex(indexPath.row)
  }
}

sorry for my bad english.

1
  • Remember to accept the answer if it solves your problem. If not leave a comment to explain why it isn't working. Thanks Commented Apr 10, 2015 at 13:03

2 Answers 2

2

You are approaching this incorrectly.

On the tableview set the property tableView.allowsMultipleSelection = true.

Then when you want to get the selected users...

let selectedIndexPaths = tableView.indexPathsForSelectedRows()

Then you can use the row property from those index paths to find the users.

var selectedUsers: [User] = []

for indexPath in selectedIndexPaths {
    let user = theUsers[indexPath.row]

    selectedUsers.append(user)
}

The multiple selection and the check mark is handled automatically by the tableview.

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

Comments

0

To follow your approach, I think this could help you ;)

But the approach of @Fogmeister is better !

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

        let tagValue = 100 + indexPath.row
        let cell: UITableViewCell = tableView.viewWithTag(tagValue) as UITableViewCell

        if cell.accessoryType == UITableViewCellAccessoryType.None
        {
            cell.accessoryType = UITableViewCellAccessoryType.Checkmark
            userToSendPost.append(self.users2[indexPath.row])
        } else {
            let tempUserString = self.users2[indexPath.row] // get your user string
            if contains(userToSendPost, tempString) { // check if exist on your array
                userToSendPost.removeObject(tempString) // remove it
            }
            cell.accessoryType = UITableViewCellAccessoryType.None
        }
}


// If you want to use Array, add this method
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!)
            }
        }
}

Comments

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.