casillas
casillas

Reputation: 16813

TextField Change event in a CustomCell

I have been working on CustomCell in Xamarin.iOS platform. The following code is working fine. I have tableItems which is List that stores Start and End values.

And I also have a class(SettingCustomCell) where I have programmatically created TextFields to display Start and End values.

I wonder how could I detect/capture when user changes either Start or End values in the TextField in my current implementation.

MainTableViewController

tableItems.Add (new TableItem() {Start=1000, End=4000});
tableItems.Add (new TableItem() {Start=4000, End=6000});

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    SettingsCustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as SettingsCustomCell;

    if (cell == null) {
        cell = new SettingsCustomCell (cellIdentifier);
    }

    cell.UpdateCell (tableItems [indexPath.Row].Start, tableItems [indexPath.Row].End);

    return cell;
}

SettingsCustomCell

public CustomCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
{
    SelectionStyle = UITableViewCellSelectionStyle.None;

    firstLabel = new UITextField ();
    secondLabel = new UITextField ();

    ContentView.Add (firstLabel);
    ContentView.Add (secondLabel);
}

public void UpdateCell (int caption, int subtitle)
{
    firstLabel.Text = caption.ToString();
    secondLabel.Text = subtitle.ToString();
}

Upvotes: 0

Views: 266

Answers (1)

Shalva Avanashvili
Shalva Avanashvili

Reputation: 899

  1. Create the eventhandler in the tablecell. And when the your UITextField text change call this eventhandler.

    public EventHandler<bool> EditingChanged;
    //some code here
    firstTextField.ValueChanged += (s, e) => {
         if(EditingChanged!=null)
             EditingChanged(this,true);
    }
    
  2. Than in your DataSource you subscribe for EditingChanged event and create another eventhandler which will be called when the EditingChanged will be executed.

      public EventHandler<bool> SourceEditingChanged;
      public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
      {
            SettingsCustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as SettingsCustomCell;
    
            if (cell == null) {
                  cell = new SettingsCustomCell (cellIdentifier);
            }
           cell.UpdateCell (tableItems [indexPath.Row].Start, tableItems [indexPath.Row].End);
    
           cell.EditingChanged += {
               if(SourceEditingChanged!=null)
                    SourceEditingChanged(this,true);
           }; 
          return cell;
      }
    
  3. Subscribe for your SourceEditingChange Event from viewController.

       MySource.SourceEditingChange += (s,e) =>{
            //Your Code here
       }
    

Upvotes: 1

Related Questions