How to add and remove rows in a UITableview dynami

2019-08-20 11:12发布

问题:

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.

Please help me...

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

回答1:

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

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

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