繁体   English   中英

如何在xamarin.ios中动态添加和删除UITableview中的行

[英]How to add and remove rows in a UITableview dynamically in xamarin.ios

我是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();
    }
  }
}

在此处输入图片说明

我要在单击加号按钮时向表中添加行,并在单击减号按钮时将所选行删除。浏览文件后,文件名也显示在标签中。

您可以使用NSNotification操作数据源,然后重新加载TableView。

单击添加按钮,数据源+ 1,单击删除按钮,数据源将删除指定的一个。

ViewController

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
}

UITableViewCell

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM