2017-09-22 75 views
0

我有一个带有2-3个部分的UITableView。我想实现一个功能,可以选择每个部分的单个行。每个UITableViewSection的单选iOS Xamarin

事情是这样: -

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

回答

0

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

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