代码之家  ›  专栏  ›  技术社区  ›  user3034944

每个UITableViewSection iOS Xamarin都有一个选择

  •  0
  • user3034944  · 技术社区  · 8 年前

    我有一个2-3节的UITableView。我想实现一个功能,其中可以从每个部分中选择一行。

    比如:-

    enter image description here

    我已尝试在UITableView上启用多项选择。但它允许我从所有部分中选择多行。我希望每次只从每个部分中选择一行。

     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;
            }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   ColeX    8 年前

     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);
    }
    

    enter image description here

    推荐文章