Select only one row in each section of a UITableView?

SMabbuttSMabbutt Member ✭✭

I'm developing a filtering system and can't for the life of me work out how to limit the user to selecting only one option in each section, and deselecting the other rows when they do attempt to select a different option.

I've got a table view that contains all possible options, under separate headings. I then have an update button which will gather the data from all the currently selected cells and query the database.

Any suggestions on the best way to deselect the existing selection in the section and update it to the new one?

Thanks,
Steve

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    Check all the selected index paths in the event WillSelectRow of UITableViewDelegate deselected any selected rows which have the same sections as the currently selected row:

    public override NSIndexPath WillSelectRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (tableView.IndexPathsForSelectedRows != null)
        {
            foreach (NSIndexPath selectedIndexPath in tableView.IndexPathsForSelectedRows)
            {
                if (selectedIndexPath.Section == indexPath.Section)
                {
                    tableView.DeselectRow(selectedIndexPath, true);
                }
            }
        }
    
        return indexPath;
    }
    

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    Check all the selected index paths in the event WillSelectRow of UITableViewDelegate deselected any selected rows which have the same sections as the currently selected row:

    public override NSIndexPath WillSelectRow(UITableView tableView, NSIndexPath indexPath)
    {
        if (tableView.IndexPathsForSelectedRows != null)
        {
            foreach (NSIndexPath selectedIndexPath in tableView.IndexPathsForSelectedRows)
            {
                if (selectedIndexPath.Section == indexPath.Section)
                {
                    tableView.DeselectRow(selectedIndexPath, true);
                }
            }
        }
    
        return indexPath;
    }
    
Sign In or Register to comment.