lundi 25 avril 2016

Cannot deselect first selected cell in tableview

I'm trying to implement a multi-selection tableview in iOS. How it works:

Every item has a checkbox on the left. When the user taps this checkbox the tableview switches to multi-selection mode. The user can now select additional items by tapping on the checkbox or anywhere in the cells. This is all working fine save for 1 thing:

Say I have 3 items in my list: A, B and C ordered alphabetically. I tap A's checkbox to enter multi-select mode and proceed to add B and C as well. If I tap B or C again the item is deselected and their respective checkboxes become unchecked again. If I attempt to deselect A before B and C are deselected however, the cell still looks selected. A does get removed from my array of selected items however. This always happens with the item I use to enter multi-select mode.

My code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyNotificationTableViewCell
    let notification = fetchedResultsController.objectAtIndexPath(indexPath) as! MyNotification

    if(tableView.allowsMultipleSelection){
        if(!selectedNotificationsArray.containsObject(notification.notification_id)){
            tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
            selectedNotificationsArray.addObject(notification.notification_id)
            cell.selectionButton.imageView?.image = UIImage(named: "CheckboxChecked")
        }
    }else{
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        let alert = UIAlertView(
            title: notification.scope,
            message: notification.message,
            delegate: self,
            cancelButtonTitle: LocalizedString("cancel"),
            otherButtonTitles: LocalizedString("alert_option_mark_as_read")
        )
        alert.tag = 1
        selectedNotificationsArray.addObject(notification.notification_id)
        alert.show()
    }

    cell.setNeedsDisplay()
}

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
    if (buttonIndex == 1) {
        markSelectedNotificationsAsRead()
    }else{
        selectedNotificationsArray.removeAllObjects()
    }
}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    print("deselected a cell")

    let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyNotificationTableViewCell
    let notification = fetchedResultsController.objectAtIndexPath(indexPath) as! MyNotification

    if(selectedNotificationsArray.containsObject(notification.notification_id)){
        selectedNotificationsArray.removeObject(notification.notification_id)
        tableView.deselectRowAtIndexPath(indexPath, animated: false)
        cell.selectionButton.imageView?.image = UIImage(named: "CheckboxUnchecked")
        if(selectedNotificationsArray.count == 0){
            switchNavigationBarMode(false)
            tableView.reloadData()
        }
    }
}

func tappedSelectionCheckbox(sender: CheckBoxButton){
    let notification = fetchedResultsController.objectAtIndexPath(sender.cellIndexPath!) as! MyNotification
    print("tapped checkbox of notification with message:  " + String(notification.message))
    if(tableView.allowsMultipleSelection == false){
        switchNavigationBarMode(true)
    }
    if(selectedNotificationsArray.containsObject(notification.notification_id)){
        tableView(tableView, didDeselectRowAtIndexPath: sender.cellIndexPath!)
    }else{
        tableView(tableView, didSelectRowAtIndexPath: sender.cellIndexPath!)
    }
    //Redraw cell so that the checkbox is updated
    tableView(tableView, cellForRowAtIndexPath: sender.cellIndexPath!).setNeedsDisplay()
}

func switchNavigationBarMode(enabled: Bool){
    if(enabled){
        tableView.allowsMultipleSelection = true
        backButton = self.navigationItem.leftBarButtonItem
        self.navigationItem.setLeftBarButtonItem(selectAllButton, animated: true)
        self.navigationItem.setRightBarButtonItems([markReadButton!, deleteButton!], animated: true)
        self.navigationItem.title = nil
    }else{
        tableView.allowsMultipleSelection = false
        self.navigationItem.title = "Notifications"
        self.navigationItem.setLeftBarButtonItem(backButton, animated: true)
        self.navigationItem.setRightBarButtonItems(nil, animated: true)
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyNotificationTableViewCell
    let notification = fetchedResultsController.objectAtIndexPath(indexPath) as! MyNotification
    cell.messageLabel.text = notification.message

    // Unread notifications are bold
    if(notification.read_date.isEmpty){
        cell.messageLabel.font = UIFont(name: "TrebuchetMS-Bold", size: 17)
    }

    if(selectedNotificationsArray.containsObject(notification.notification_id)){
        tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)
        cell.selectionButton.imageView?.image = UIImage(named: "CheckboxChecked")

    }else{
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        cell.selectionButton.imageView?.image = UIImage(named: "CheckboxUnchecked")
    }
    cell.selectionButton.cellIndexPath = indexPath
    cell.selectionButton.addTarget(self, action: #selector(MyNotificationViewController.tappedSelectionCheckbox(_:)), forControlEvents: UIControlEvents.TouchUpInside)

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S'Z'"
    let utcDateTimeObject = dateFormatter.dateFromString(notification.send_date)
    dateFormatter.dateFormat = "HH:mm"
    let shortTimestamp = dateFormatter.stringFromDate(utcDateTimeObject!)
    cell.timestampLabel.text = shortTimestamp
    let scope = notification.scope
    switch(scope){
    case Constants.NOTIFICATION_SCOPE_DEVICE:

        break
    case Constants.NOTIFICATION_SCOPE_INVENTORY:

        break
    case Constants.NOTIFICATION_SCOPE_JOURNAL:

        break
    case Constants.NOTIFICATION_SCOPE_PROTOCOL:

        break
    case Constants.NOTIFICATION_SCOPE_SUPPLIES:

        break
    case Constants.NOTIFICATION_SCOPE_SYSTEM:

        break
    default:
        break
    }
    return cell
}




Aucun commentaire:

Enregistrer un commentaire