我是xamarin.ios的新手,我想在按钮单击中将行添加到UITableView中,并在单击特定按钮时删除该行。
请帮帮我......
我的viewcontroller.cs
public partial class ViewController : UIViewController
{
public static UITableView table;
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
table = new UITableView(new CGRect(0, 0, 500, 500));
View.AddSubview(table);
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("Cell"), OnChange, this);
contentdata _cnt = new contentdata();
table.AddSubview(_cnt);
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
}
private void Addmore_TouchUpInside(object sender,
EventArgs e)
{
NSNotificationCenter.DefaultCenter.PostNotificationName("Cell", new NSString("Add"));
}
public class contentdata : UIView
{
public contentdata()
{
this.Frame = new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, 50);
nfloat width = UIScreen.MainScreen.Bounds.Width / 3;
UITextField title = new UITextField();
title.Placeholder = " Enter title";
title.TextColor = UIColor.Black;
title.TextAlignment = UITextAlignment.Left;
title.Font = UIFont.SystemFontOfSize(20);
title.Layer.BorderColor = UIColor.Black.CGColor;
title.Layer.BorderWidth = 1;
title.Frame = new CGRect(10, 0, width, 75);
this.Add(title);
UIView _view2 = new UIView(new CGRect(0,5,10,10));
_view2.BackgroundColor = UIColor.White;
_view2.Layer.BorderColor = UIColor.Black.CGColor;
_view2.Layer.BorderWidth = 1;
_view2.Frame = new CGRect(width, 0, UIScreen.MainScreen.Bounds.Width / 3, 75);
this.Add(_view2);
nfloat _view2Width = _view2.Frame.Width;
nfloat _view2Height = _view2.Frame.Height;
UIButton _browse = new UIButton(new CGRect(15, 10,100, 30));
_browse.SetTitle("Browse", UIControlState.Normal);
// _browse.SizeToFit();
_browse.SetTitleColor(UIColor.Black, UIControlState.Normal);
_browse.Layer.CornerRadius = 13;
_browse.Layer.BorderWidth = 2;
_browse.Layer.BorderColor = UIColor.FromRGB(25, 106, 155).CGColor;
// _browse.TouchUpInside += _browse_TouchUpInside;
_view2.AddSubview(_browse);
UILabel _text = new UILabel(new CGRect(5, (_view2Height/2)+5, 150, 30));
_text.Text = "No file selected";
_text.TextAlignment = UITextAlignment.Justified;
_text.TextColor = UIColor.Black;
_text.Font = _text.Font.WithSize(15);
_text.LineBreakMode = UILineBreakMode.WordWrap;
_view2.AddSubview(_text);
UIView _view3 = new UIView();
_view3.BackgroundColor = UIColor.White;
_view3.Layer.BorderColor = UIColor.Black.CGColor;
_view3.Layer.BorderWidth = 1;
_view3.Frame = new CGRect(width * 2, 0, UIScreen.MainScreen.Bounds.Width / 3-10, 75);
this.Add(_view3);
UIButton _addmore = new UIButton(new CGRect(10, 15, 40, 40));
_addmore.SetTitle("+", UIControlState.Normal);
_addmore.BackgroundColor = UIColor.FromRGB(25, 106, 155);
_addmore.SetTitleColor(UIColor.White, UIControlState.Normal);
_addmore.TouchUpInside += Addmore_TouchUpInside;
_view3.AddSubview(_addmore);
UIButton _remove = new UIButton(new CGRect(65, 15, 40, 40));
_remove.BackgroundColor = UIColor.Red;
_remove.SetTitle("-", UIControlState.Normal);
_remove.SetTitleColor(UIColor.White, UIControlState.Normal);
_view3.AddSubview(_remove);
}
void OnChange(NSNotification notification)
{
string s = notification.Object as NSString;
if (s == "Add")
{
contentdata _cd = new AddMore.ViewController.contentdata();
table.AddSubview(_cd);
}
else
{
int index = (notification.Object as NSIndexPath).Row;
//tableItems.RemoveAt(index);
}
table.ReloadData();
}
}
}
我想在单击加号按钮时向表中添加行,并在单击减号按钮时删除所选行。浏览文件后,文件名也会显示在标签中。如何执行此操作< / p>
答案 0 :(得分:0)
您可以使用NSNotification
来操作数据源,然后重新加载TableView。
单击添加按钮,数据源+ 1,单击删除按钮时,数据源将删除指定的数据。
void OnChange(NSNotification notification)
{
string s = notification.Object as NSString;
if(s is "Add")
{
tableItems.Add(new TableItem("Vegetables") { SubHeading = "65 items", ImageName = "Vegetables.jpg" });
}
else
{
int index = (notification.Object as NSIndexPath).Row;
tableItems.RemoveAt(index);
}
table.ReloadData();
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("Cell"), OnChange,this);
//xxx
}
private void _addmore_TouchUpInside(object sender, EventArgs e)
{
NSNotificationCenter.DefaultCenter.PostNotificationName("Cell", new NSString("Add"));
}
private void _remove_TouchUpInside(object sender, EventArgs e)
{
NSNotificationCenter.DefaultCenter.PostNotificationName("Cell", Cellindex);
}