0

J'ai un UITableView avec 2-3 sections. Je veux mettre en œuvre une fonctionnalité où une seule ligne de chaque section peut être sélectionnée.Sélection unique pour chaque UITableViewSection iOS Xamarin

Quelque chose comme ceci: -

enter image description here

J'ai essayé permettant une sélection multiple sur le UITableView. Mais cela me permet de sélectionner plusieurs lignes dans toutes les sections. Je veux sélectionner seulement une ligne à la fois de chaque section.

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

Répondre

0

Vous pouvez utiliser une liste pour stocker des indicateurs pour chaque section que vous avez sélectionnée la dernière fois.

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