我正在使用Xamarin开发IOS应用程序。我已经定义了我的表视图,并为每个单元的AccessoryVenterView设置了UISwitch 我需要整理所有单元格并显示其开关打开的单元格的单元格数据
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();
}
}
谢谢
答案 0 :(得分:1)
您需要检查您的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替换。