2016-04-25 52 views
0

我试图在iOS中实现多选表格视图。它是如何工作的:无法在tableview中取消选择第一个选定的单元格

每个项目的左边都有一个复选框。 当用户点击此复选框时,tableview切换到多选模式。 用户现在可以通过点击复选框或单元格中的任何位置来选择其他项目。这一切都工作正常保存1件事:

说我有3项在我的名单:A,B和C按字母顺序排列。我点击A的复选框进入多选模式并继续添加B和C.如果我再次点击B或C,则取消选中该项目,并且它们各自的复选框会再次被取消选中。如果我在取消选择B和C之前尝试取消选择A,则该单元看起来仍处于选中状态。然而,A会从我选择的项目中移除。这通常发生在我用来进入多选模式的项目中。

我的代码:

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 
} 

回答

0

您在didSelectRowAtIndexPath方法调用tableView.selectRowAtIndexPath。我希望这会导致一些错误的行为。

我会建议一个不同的更简单的方法。如果你使用UITableViewCellAccessoryCheckmark来显示你选择的单元格,cellForRowAtIndexPath可以检查A,B,C等是否在你的数据数组中,并在那里选择并添加选中标记。然后,你只需要在didDeselectRowAtIndexPath内调用reloadData,它更清晰。

这会稍微改变你的用户界面,所以如果你严格地希望使用灰色选定的样式单元格,这将不起作用,但问题似乎是在didDeselectRowAtIndexPath中调用selectRowAtIndexPath。你应该找到另一种方法来选择单元格。

+0

“你可以在didDeselectRowAtIndexPath中调用tableView.selectRowAtIndexPath,我期望这会导致一些错误行为。” 我做...?你的意思是说我隐式地称它?因为我的didDeselectRowAtIndexPath没有直接调用selectRowAtIndexPath。 – Anubis

+0

看起来像你在第7行调用它。 –

+0

但是,这是在didSelectRowAtIndexPath方法,而不是您在答案中提到的didDeselectRowAtIndexPath方法。 – Anubis

相关问题