使用Xamarin检查表格单元格中UI开关的状态



我正在使用Xamarin开发IOS应用程序。我已经定义了我的表视图,并为每个单元格的AccessoryVenterView设置了UISwitch我需要对所有单元格进行交互,并显示其开关为ON 的单元格的单元格数据

var indexPaths = table.IndexPathsForVisibleRows;
                foreach (var indexPath in indexPaths) 
                {
                    var cell = table.CellAt (indexPath);
                    if (/*cell.AccessoryView.isOn*/)
                    {
                        new UIAlertView ("On", "This cell has its switch on and its value is "/*+CELL_DATA*/, null, "OK", null).Show();
                    }
                }

感谢

您需要检查AccessoryView是否属于UISwitch类型。

var indexPaths = table.IndexPathsForVisibleRows;
foreach (var indexPath in indexPaths) 
{
    var cell = table.CellAt (indexPath);
    var switchView = cell.AccessoryView as UISwitch;
    if(switchView == null)
       continue;
    if (switchView.On)
    {
        // if it is a default cell you get the text like this
        var cellText = cell.TextLabel.Text;
        new UIAlertView ("On", "This cell has its switch on and its value is " + cellText, null, "OK", null).Show();
    }
}

这适用于默认单元格。如果使用自定义单元格,则需要将单元格强制转换为您的单元格类型。因此var cell = table.CellAt (indexPath);变成var cell = (MyCustomCell)table.CellAt (indexPath);。然后您就可以访问自定义单元格的属性。

还请注意,UIAlertView已取消验证,应替换为AlertViewController。

最新更新