每个 UITableViewSection iOS Xamarin 的单一选择

Single selection for every UITableViewSection iOS Xamarin

我有一个包含 2-3 个部分的 UITableView。我想实现一个功能,其中每个部分的单行都可以 selected。

像这样:-

我尝试在 UITableView 上启用多个 selection。但它允许我从所有部分中 select 多行。我想 select 每个部分一次只显示一行。

 public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.CellAt(indexPath);


                if (cell.Accessory == UITableViewCellAccessory.None)
                {
                    cell.Accessory = UITableViewCellAccessory.Checkmark;
                }
                else
                {
                    cell.Accessory = UITableViewCellAccessory.None;
                }


            selectedSection = indexPath.Section;

        }
        public override void RowDeselected(UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.CellAt(indexPath);
            cell.Accessory = UITableViewCellAccessory.None;
        }

您可以使用列表来存储您上次选择的每个部分的标志。

 List<NSIndexPath> selectList = new List<NSIndexPath>();
 for(int i = 0; i < tableviewDatasource.Count; i++)
 {
      //initial index 0 for every section
      selectList.Add(NSIndexPath.FromRowSection(0, i));
 }

public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
    //anti-highlight last cell
    NSIndexPath lastindex = selectList[indexPath.Section];
    var lastcell = tableView.CellAt(lastindex);
    lastcell.Accessory = UITableViewCellAccessory.None;

    //highlight selected cell
    var cell = tableView.CellAt(indexPath);
    cell.Accessory = UITableViewCellAccessory.Checkmark;

    //update the selected index
    selectList.RemoveAt(indexPath.Section);
    selectList.Insert(indexPath.Section, indexPath);
}