markith
markith

Reputation: 3

Use button to create/append array

EDIT 2: Thought I had it but I don't. I need to create the array when the button is pressed, but I can't figure out how to access the .checkmark for each object. I have ".selected" as a property already, but can't access that either.

I have a UITableView that display a list in three sections with checkmarks. When a row in checked, the bool associated with that NSObject class changes from false to true. I have a button in the UINavigationBar that when pressed shows an alert with "Send" and "Cancel" options. When the user taps "Send" I want to have all the rows with checkmarks added into an array. I figured using that bool would be the best way, but in the func for the action I can't call the bool associated with each NSObject in the array. I included the parts of code I think are needed to help me.

EDIT: I believe this is how I have my class set up, unless I'm misunderstanding. One of the class' vars is "var selected: Bool" then when I create an Item from that class I set it to false. I have a list of Items that displays fine in cellForRowAt but when the button is tapped I can't get access to that same "sortedList". Here's more code. Maybe I'm still missing something else?

var arrayOfItemsSelected = [String]()

var sortedItems = [[Item]]()
var itemList: ItemList! {
    didSet {
        sortedItems = itemList.sortByBrands()
        self.tableView.reloadData()
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let item = sortedItems[indexPath.section][indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "UIItemViewCell", for: indexPath)
    cell.textLabel?.text = "\(item.name)"
    cell.detailTextLabel?.text = "\(item.style)"

    cell.accessoryType = cell.isSelected ? .checkmark : .none
    cell.selectionStyle = .none // to prevent cells from being "highlighted"

    return cell
}

    func confirmButtonPressedAction() {

    // Create the alert controller
    let alertController = UIAlertController(title: "List of checked items", message: stringOfSelectedItems, preferredStyle: .alert)

    // Create the actions
    let okAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.default) {
        UIAlertAction in

        self.arrayOfItemsSelected.removeAll()
        self.tableView.reloadData()
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
        UIAlertAction in
    }

    // Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

    // Present the controller
    self.present(alertController, animated: true, completion: nil)
}

Upvotes: 0

Views: 135

Answers (4)

Alain T.
Alain T.

Reputation: 42143

Your code either isn't doing anything with arrayOfItemsSelected or you're not showing us the relevant parts. As it is, the code sample will neither obtain nor process the selected items.

You could either implement the didSelectRowAt and didDeselectRowAt function to maintain an internal flag in your Item class, or you could get the selection from indexPathsForSelectedRows.

If you want to completely circumvent the UITableView's selection mechanism, you would need to handle cell clicking/tapping with actions in the UIItemViewCell itself (but that seems overly complicated).

BTW, I am doubtful that you can rely on cell.isSelected from a cell you just dequeued. Also, resorting and reloading the tableview in a didSet closure is likely to get you into trouble causing performance issues, random crashes or infinite loops.

Upvotes: 0

Tony
Tony

Reputation: 59

You have to add boolean check with each array object, for example myModel.

[
"Name": "abc",
"Status": true

]

When a row in checked,

 myModel.Status = myModel.Status ? false : true

myModel's default state = false

then " you can call the bool associated with each NSObject in the array"

Upvotes: 0

Som Parkash
Som Parkash

Reputation: 234

You have to add boolean check with each array object.link below

For Objective C:

@[
  @{
     @"Name":@"abc",
     @"Status":[NSNumber numberWithBool:YES]
   }
]

For Swift:

[
  [
    "Name": "abc",
    "Status": true
  ]
]

Upvotes: 0

pesch
pesch

Reputation: 1996

If you have a property checked on the class Item you can try:

let itemsChecked = sortedItems.filter { $0.checked == true }

Upvotes: 0

Related Questions