如何在 xamarin.ios 中动态添加和删除 UITableview 中的行 [英] How to add and remove rows in a UITableview dynamically in xamarin.ios

查看:31
本文介绍了如何在 xamarin.ios 中动态添加和删除 UITableview 中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 xamarin.ios 的新手,我想通过单击按钮将行添加到 UITableView 中,并在单击特定按钮时删除该行.

I am new in xamarin.ios ,I want to add rows into the UITableView in a button click, and remove the row when clicking the particular button also.

请帮帮我...

我的 viewcontroller.cs

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

我想在单击加号按钮时将行添加到表格中,并在单击减号按钮时删除选定的行.浏览文件后,文件名也显示在标签中.我该怎么做

I want to add rows in to the table when clicking the plus button, and remove the selected row when clicking the minus button.And after browsing a file, the filename is displayed in the label also.How can I do this

推荐答案

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

You can use NSNotification to operate the datasource , and then reload the TableView.

点击添加按钮时,数据源+1,点击删除按钮时,数据源将删除指定的一项.

When click add button , datasource + 1, when click remove button, datasource will remove the specified one.

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

这篇关于如何在 xamarin.ios 中动态添加和删除 UITableview 中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆