gpt4 book ai didi

ios - CustomCell 中的 TextField Change 事件

转载 作者:行者123 更新时间:2023-11-29 01:25:54 26 4
gpt4 key购买 nike

我一直在 Xamarin.iOS 平台上开发 CustomCell。以下代码工作正常。我有 tableItems,它是存储 StartEnd 值的列表。

我还有一个类 (SettingCustomCell),我在其中以编程方式创建了 TextFields 以显示 StartEnd 值。

我想知道在我当前的实现中,当用户更改 TextField 中的 StartEnd 值时,我如何检测/捕获。

主 TableView Controller

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

设置自定义单元格

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

最佳答案

  1. 在表格单元格中创建事件处理程序。当您的 UITextField 文本更改时调用此事件处理程序。

    public EventHandler<bool> EditingChanged;
    //some code here
    firstTextField.ValueChanged += (s, e) => {
    if(EditingChanged!=null)
    EditingChanged(this,true);
    }
  2. 在您的 DataSource 中订阅 EditingChanged 事件并创建另一个事件处理程序,该事件处理程序将在执行 EditingChanged 时调用。

      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. 从 viewController 订阅您的 SourceEditingChange 事件。

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

关于ios - CustomCell 中的 TextField Change 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34026041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com