Xamarin.ios 只有UITableViewCell中的最后一个UISwitch才会触发
我试图在每个单元格中添加一个UISwitch(这是可行的)。当我点击切换除最后一个开关以外的任何开关时,它们都会给出最后一个开关的状态,直到我更改最后一个开关的状态Xamarin.ios 只有UITableViewCell中的最后一个UISwitch才会触发,xamarin.ios,Xamarin.ios,我试图在每个单元格中添加一个UISwitch(这是可行的)。当我点击切换除最后一个开关以外的任何开关时,它们都会给出最后一个开关的状态,直到我更改最后一个开关的状态 `public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) { //---- declare vars UITableViewCell cell = ta
`public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
//---- declare vars
UITableViewCell cell = tableView.DequeueReusableCell (this._cellIdentifier);
//---- if there are no cells to reuse, create a new one
if (cell == null) {
// This changes the style of the UITableViewCell to the Default style
cell = new UITableViewCell (UITableViewCellStyle.Default, this._cellIdentifier);
// Instantiate a cell accessory here
uiSwitch = new UISwitch (new RectangleF (0f, 0f, 20f, 20f));
uiSwitch.Tag = indexPath.Row;
uiSwitch.ValueChanged += (object sender, EventArgs e) => {
Console.WriteLine ("Cell Switch value is now {0}", uiSwitch.On);
};
_vRMs.View.AddSubview (uiSwitch);
// keep a reference to each cell you create,
// e.g. add them to a static List<UITableViewCell>.
// The GC won't be able to collect them so the event handler will work later.
cells.Add (cell);
}
//---- create a shortcut to our item
TableViewItem item = this._TableViewItemGroupList[indexPath.Section].Items[indexPath.Row];
cell.TextLabel.Text = item.Name;
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
cell.AccessoryView = uiSwitch;
// cell.DetailTextLabel.Text = item.SubHeading;
return cell;
}`
公共覆盖UITableViewCell GetCell(UITableView tableView,NSIndexPath indexPath){
//----申报VAR
UITableViewCell单元=tableView.DequeueReusableCell(此.\u单元标识符);
//----如果没有可重用的单元,请创建一个新单元
if(单元格==null){
//这会将UITableViewCell的样式更改为默认样式
单元格=新的UITableViewCell(UITableViewCellStyle.Default,this.\u cellIdentifier);
//在这里实例化一个单元格附件
uiSwitch=新uiSwitch(新矩形F(0f,0f,20f,20f));
uiSwitch.Tag=indexPath.Row;
uiSwitch.ValueChanged+=(对象发送方,事件参数e)=>{
Console.WriteLine(“单元格开关值现在为{0}”,uiSwitch.On);
};
_vRMs.View.AddSubview(uiSwitch);
//保留对创建的每个单元格的引用,
//例如,将它们添加到静态列表中。
//GC将无法收集它们,因此事件处理程序将在稍后工作。
单元格。添加(单元格);
}
//----创建项目的快捷方式
TableViewItem item=this.\u TableViewItemGroupList[indexPath.Section].Items[indexPath.Row];
cell.TextLabel.Text=项目名称;
cell.accessority=UITableViewcellAccessority.DisclosureIndicator;
cell.AccessoryView=ui开关;
//cell.DetailTextLabel.Text=item.SubHeading;
返回单元;
}`
我想知道是否所有这些代码都是创建一个带有UISwitches的表所必需的——对于iPhone开发人员来说,这是一个新手,我不确定。
我希望这次更新能对我的事业有所帮助
`using System;
using System.Drawing;
using System.Collections.Generic;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace eOneRaw {
public partial class vRMs : UIViewController {
#region FIELDS
private string _ViewTitle = "RMs";
private UITableView _TableView;
private TableViewDataSource _TableViewDataSource;
private List<TableViewItemGroup> _TableViewItemGroupList;
private static vRMs _vRMs;
#endregion
#region PROPERTIES
#endregion
#region ViewDidLoad
public override void ViewDidLoad () {
base.ViewDidLoad ();
// Title the Controller
Title = _ViewTitle;
#region UITableView Setup
// Set up the table and data
this.CreateTableItems ();
// Create the UITableView
_TableView = new UITableView() {
Delegate = new TableViewDelegate(_TableViewItemGroupList),
DataSource = _TableViewDataSource,
AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth,
};
_TableView.SizeToFit();
// Reposition and resize the receiver
_TableView.Frame = new RectangleF (0, 0, this.View.Frame.Width, this.View.Frame.Height);
// Add the table view as a subview
this.View.AddSubview(_TableView);
#endregion
#region Define the Look of the View
var _barbtnCancel = new UIBarButtonItem(UIBarButtonSystemItem.Done);
NavigationItem.LeftBarButtonItem = _barbtnCancel;
_barbtnCancel.Clicked += (s, e) => {
this.NavigationController.PopViewControllerAnimated(true);
};
#endregion
} // end ViewDidLoad
#endregion
#region METHODS
public vRMs () {
// Shouldn't ever happen
_vRMs = this;
Console.WriteLine (Environment.StackTrace);
}
public override void DidReceiveMemoryWarning () {
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
#endregion
#region ALL TABLE FUNCTIONALITY
#region CreateTableItems
//========================================================================
/// <summary>
/// Creates a set of table items.
/// </summary>
// This is where you define your table
protected void CreateTableItems () {
_TableViewItemGroupList = new List<TableViewItemGroup> ();
//---- declare vars
TableViewItemGroup tGroup;
tGroup = new TableViewItemGroup() { Name = "Regional Managers" };
tGroup.Items.Add (new TableViewItem() { Name = "Chris" });
tGroup.Items.Add (new TableViewItem() { Name = "Mike" });
tGroup.Items.Add (new TableViewItem() { Name = "Dan" });
tGroup.Items.Add (new TableViewItem() { Name = "Steve" });
_TableViewItemGroupList.Add (tGroup);
this._TableViewDataSource = new TableViewDataSource(_TableViewItemGroupList);
}
#endregion
#region CLASS TableViewDelegate
// The delegate manages the transitions from view-to-view
private class TableViewDelegate : UITableViewDelegate {
private List<TableViewItemGroup> _TableViewItemGroupList;
public TableViewDelegate(List<TableViewItemGroup> pList) {
this._TableViewItemGroupList = pList;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath) {
return;
}
}
#endregion
#region CLASS TableViewDataSource
public class TableViewDataSource : UITableViewDataSource {
protected List<TableViewItemGroup> _TableViewItemGroupList;
string _cellIdentifier = "TableViewCell";
private static UISwitch uiSwitch;
static List<UITableViewCell> cells = new List<UITableViewCell> ();
public TableViewDataSource (List<TableViewItemGroup> items) {
this._TableViewItemGroupList = items;
}
/// <summary>
/// Called by the TableView to determine how many sections(groups) there are.
/// </summary>
public override int NumberOfSections (UITableView tableView) {
return this._TableViewItemGroupList.Count;
}
/// <summary>
/// Called by the TableView to determine how many cells to create for that particular section.
/// </summary>
public override int RowsInSection (UITableView tableview, int section) {
return this._TableViewItemGroupList[section].Items.Count;
}
/// <summary>
/// Called by the TableView to retrieve the header text for the particular section(group)
/// </summary>
public override string TitleForHeader (UITableView tableView, int section) {
return this._TableViewItemGroupList[section].Name;
}
/// <summary>
/// Called by the TableView to retrieve the footer text for the particular section(group)
/// </summary>
public override string TitleForFooter (UITableView tableView, int section) {
return this._TableViewItemGroupList[section].Footer;
}
#region UITableViewCell
/// <summary>
/// Called by the TableView to get the actual UITableViewCell to render for the particular section and row
/// </summary>
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
//---- declare vars
UITableViewCell cell = tableView.DequeueReusableCell (this._cellIdentifier);
//---- if there are no cells to reuse, create a new one
if (cell == null) {
// This changes the style of the UITableViewCell to the Default style
cell = new UITableViewCell (UITableViewCellStyle.Default, this._cellIdentifier);
// This changes the style of the UITableViewCell to the Subtitle style,
// which displays a second line of text within the cell.
// cell = new UITableViewCell (UITableViewCellStyle.Subtitle, this._cellIdentifier);
// Instantiate a cell accessory here
uiSwitch = new UISwitch (new RectangleF (0f, 0f, 20f, 20f));
uiSwitch.Tag = indexPath.Row;
uiSwitch.ValueChanged += (object sender, EventArgs e) => {
Console.WriteLine ("Cell Switch value is now {0}", uiSwitch.On);
};
_vRMs.View.AddSubview (uiSwitch);
// keep a reference to each cell you create,
// e.g. add them to a static List<UITableViewCell>.
// The GC won't be able to collect them so the event handler will work later.
cells.Add (cell);
}
//---- create a shortcut to our item
TableViewItem item = this._TableViewItemGroupList[indexPath.Section].Items[indexPath.Row];
cell.TextLabel.Text = item.Name;
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
cell.AccessoryView = uiSwitch;
// cell.DetailTextLabel.Text = item.SubHeading;
// Add an image if needed
/*
if(!string.IsNullOrEmpty(item.ImageName))
{
cell.ImageView.Image = UIImage.FromFile("Images/" + item.ImageName );
}
*/
return cell;
}
#endregion
} // end TableViewDataSource Class
#endregion
#region CLASS TableViewItemGroup
//========================================================================
/// <summary>
/// A group that contains table items
/// </summary>
public class TableViewItemGroup {
public string Name { get; set; }
public string Footer { get; set; }
public List<TableViewItem> Items {
get { return this._items; }
set { this._items = value; }
}
protected List<TableViewItem> _items = new List<TableViewItem>();
public TableViewItemGroup () {
}
}
#endregion
#region CLASS TableViewItem
//========================================================================
/// <summary>
/// Represents our item in the table
/// </summary>
public class TableViewItem {
public string Name { get; set; }
public string SubHeading { get; set; }
public string ImageName { get; set; }
public TableViewItem () {
}
}
#endregion
#endregion
#region OBSOLETE methods
// ***************************** OBSOLETE
// ***************************** OBSOLETE
// ***************************** OBSOLETE
[Obsolete]
public override void ViewDidUnload () {
base.ViewDidUnload ();
// Clear any references to subviews of the main view in order to
// allow the Garbage Collector to collect them sooner.
//
// e.g. myOutlet.Dispose (); myOutlet = null;
ReleaseDesignerOutlets ();
}
[Obsolete]
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) {
// Return true for supported orientations
return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown);
}
#endregion
}
}`
`使用系统;
使用系统图;
使用System.Collections.Generic;
使用单调的基础;
使用MonoTouch.UIKit;
命名空间eOneRaw{
公共部分类vRMs:UIViewController{
#区域字段
私有字符串_ViewTitle=“RMs”;
私有UITableView _TableView;
私有TableViewDataSource _TableViewDataSource;
私有列表_TableViewItemGroupList;
专用静态VRMSu vRMs;
#端区
#区域属性
#端区
#区域视图加载
公共覆盖无效ViewDidLoad(){
base.ViewDidLoad();
//控制者的头衔
标题=_视图标题;
#区域UITableView设置
//设置表格和数据
this.CreateTableItems();
//创建UITableView
_TableView=新UITableView(){
委托=新的TableViewDelegate(_TableViewItemGroupList),
数据源=_TableViewDataSource,
AutoresizingMask=UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth,
};
_TableView.SizeToFit();
//重新定位接收器并调整其大小
_TableView.Frame=新矩形F(0,0,this.View.Frame.Width,this.View.Frame.Height);
//将表视图添加为子视图
this.View.AddSubview(_TableView);
#端区
#区域定义视图的外观
var_barbtnCancel=新的UIBarButtonItem(UIBarButtonSystemItem.Done);
NavigationItem.LeftBarButtonItem=\u barbtnCancel;
_点击+=(s,e)=>{
this.NavigationController.PopViewControllerAnimated(true);
};
#端区
}//结束视图加载
#端区
#区域方法
公共车辆登记管理系统(){
//不应该发生
_vRMs=此;
Console.WriteLine(Environment.StackTrace);
}
public override void direceivememorywarning(){
//如果视图没有superview,则释放该视图。
base.DidReceiveMemoryWarning();
//释放所有未使用的缓存数据、图像等。
}
#端区
#区域所有表功能
#区域CreateTableItems
//========================================================================
///
///创建一组表项。
///
//这是定义表的地方
受保护的void CreateTableItems(){
_TableViewItemGroupList=新列表();
//----申报VAR
TableViewItemGroup tGroup;
tGroup=new TableViewItemGroup(){Name=“Regional Managers”};
tGroup.Items.Add(新的TableViewItem(){Name=“Chris”});
tGroup.Items.Add(新的TableViewItem(){Name=“Mike”});
tGroup.Items.Add(新的TableViewItem(){Name=“Dan”});
tGroup.Items.Add(新的TableViewItem(){Name=“Steve”});
_TableViewItemGroupList.Add(tGroup);
这是._TableViewDataSource=新的TableViewDataSource(_TableViewItemGroupList);
}
#端区
#区域类TableViewDelegate
//委托管理视图之间的转换
私有类TableViewDelegate:UITableViewDelegate{
私有列表_TableViewItemGroupList;
公共TableViewDelegate(列表pList){
这是._TableViewItemGroupList=pList;
}
public override void RowSelected(UITableView tableView,NSIndexPath indexPath){
返回;
}
}
#端区
#区域类TableViewDataSource
公共类TableViewDataSource:UITableViewDataSource{
受保护列表\u TableViewItemGroupList;
字符串_cellIdentifier=“TableViewCell”;
专用静电开关;
静态列表