gpt4 book ai didi

c# - TableView 单元格在滚动时重置其数据

转载 作者:行者123 更新时间:2023-11-29 01:51:04 33 4
gpt4 key购买 nike

-----编辑-----

通过在 TableViewSource 中使用 DraggingStarted 方法,我找到了解决问题的方法。这不是最佳解决方案,我会继续寻找合适的方法来处理我的问题,但现在,我可以继续我的过程。

public override void DraggingStarted(UIScrollView scrollView)
{
for (int i = 0; i < 12; i++)
{
try
{
GlobalSupport.NewClientData[i] = ((scrollView as UITableView).CellAt(NSIndexPath.FromItemSection(i, 0)).ContentView.Subviews[1] as UITextField).Text;
}
catch
{
}
}

Console.WriteLine("hoi");
}

然后在我的 GetCell 方法中搜索这个新的数据结构,以找到正确的值:

if (GlobalSupport.NewClientData[indexPath.Row] != "")
{
cell.cellValue = GlobalSupport.NewClientData[indexPath.Row];
}

-----结束-----

我正在努力实现一个以 TableView 为中心的应用程序。这意味着我的大部分屏幕都是 TableView,其中包含可重复使用的单元格。这些单元格是使用 Xcode Designer 定义的(这意味着我的资源中有一个 .cs、一个 .xib 和一个 .designer 用于每个不同的唯一单元格)。

当使用预定义数据(自定义数据对象、字典、列表等)构建 TableView 时,我调用单元格的 .cs 中的方法来修改 .xib 中定义的两个 UILabel(或 UILabel 和 UIImageView) .这一切顺利;提供的数据通过我的 TableViewSource 中的 GetCell 方法放入标签和其他控件中,并保留在那里,即使在滚动后也是如此(因为数据是预定义的,并使用 IndexPath.Row 来确定放置位置)。

麻烦来了:

我有另一个单元格,其中有一个 UILabel 和 UITextField。 UILabel 的文本已修改为与我的可本地化字符串文件相对应。我的 GetCell 方法中有一个开关,在我的 TableViewSource 中,它看起来像这样:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
CTextInput cell = (CTextInput)tableView.DequeueReusableCell(CTextInput.Identifier);

switch (indexPath.Row)
{
case 0:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtBSN", "", "");
break;
case 1:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtPrefix", "", "");
break;
case 2:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtInitials", "", "");
break;
case 3:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtFirstName", "", "");
break;
case 4:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtLastName", "", "");
break;
case 5:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtDateOfBirth", "", "");
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
break;
case 6:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtCity", "", "");
break;
case 7:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtStreet", "", "");
break;
case 8:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtHouseNumber", "", "");
break;
case 9:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtZipcode", "", "");
break;
case 10:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtCountry", "", "");
break;
case 11:
cell.cellLabel = NSBundle.MainBundle.LocalizedString("txtPhonenumber", "", "");
break;
default:
break;
}

return cell;
}

效果很好。

现在,加载 View 后,用户可以在每个 UILabel 旁边的 UITextField 中输入他或她的数据。提供数据,当用户在 TableView 中向下移动时,所有文本都保留在那里......直到用户到达屏幕底部,并且需要滚动以查看其他单元格。然后,前几个单元格的 UITextField 中提供的数据被重置为单元格的 .xib 文件中指定的值,即“”。这是因为我的 GetCell 方法开头的 DequeueReusableCell。每次新单元格进入用户 View 时都会调用 GetCell 方法。这意味着所有从用户 View 中消失的单元格都将重置为默认值。

Supply the data

Scrolling down

Scrolling back up

我不希望此 View 中的每个单元格都有一组单独的 .cs、.xib 和 .designer 文件,并且到目前为止还没有找到保存单元格的 UITextField 内容的方法。更重要的是,我需要每个单元格内的 UITextField 的内容,以便创建一个新对象,我需要进一步推进这个过程。

非常感谢有关此主题的任何帮助。如果您需要更多信息,请随时与我联系!

尊敬的问候,比约恩

最佳答案

UITableView 的数据源需要为单元格提供完整且正确呈现所需的任何数据。由于单元格被重用,因此它们可以被认为是“哑的”并且完全依赖于数据源来知道要显示什么。

记住这一点,您需要做的是更新数据源以包含输入到单元格的 UITextField 中的任何内容。

以下是如何实现此目的的示例:

1.) 将 UITableView 的数据源实现为键/值对:

DataSource = new Dictionary<string, string> () {
{ "Label 1", string.Empty },
{ "Label 2", string.Empty },
...
...
};

对于您的场景,“标签 1”将为 NSBundle.MainBundle.LocalizedString("txtBSN", "", "")

2.) 实现您的 CTextInput像这样的单元格:

public partial class CTextInput : UITableViewCell
{
Action<string, string> OnTextFieldTextChanged;

public CustomCell ()
{
}

public CustomCell (IntPtr handle) : base (handle)
{
}

public void SetupCell (string labelText, string value, Action<string, string> onTextFieldTextChanged)
{
CellLabel.Text = labelText;
CellTextField.Text = value;
OnTextFieldTextChanged = onTextFieldTextChanged;

CellTextField.EditingDidEnd += HandleEditingDidEnd;
}

void HandleEditingDidEnd (object sender, EventArgs e)
{
var textField = sender as UITextField;
OnTextFieldTextChanged (CellLabel.Text, textField.Text);
}

// clean up
public override void PrepareForReuse ()
{
base.PrepareForReuse ();

OnTextFieldTextChanged = null;
CellTextField.EditingDidEnd -= HandleEditingDidEnd;
}

protected override void Dispose (bool disposing)
{
if (disposing) {
OnTextFieldTextChanged = null;
}

base.Dispose (disposing);

}

}

这里重要的是 SetupCell方法和 HandleEditingDidEnd事件处理程序。您 Hook 到 UITextField 的 EditingDidEnd ,以便可以调用传入的 onTextFieldTextChanged行动。

3.) 添加 OnTextFieldTextChange处理程序并实现 GetCell View Controller 中的方法如下:

public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell ("CellIdentifier") as CTextInput ?? new CTextInput ();

var labelText = DataSource.Keys.ElementAt (indexPath.Row);
var textFieldText = DataSource.Values.ElementAt (indexPath.Row);

cell.SetupCell (labelText, textFieldText, OnTextFieldTextChange);

return cell;
}


public void OnTextFieldTextChange (string key, string value)
{
if (DataSource.ContainsKey (key)) {
DataSource [key] = value;
}
}

这里你定义了一个OnTextFieldTextChange可以传入 Action<string, string> onTextFieldTextChanged 的方法SetupCell 中的参数之前在 CTextInput 中定义的方法类(class)。

OnTextFieldTextChange方法中您所做的就是更新数据源中给定键的值。

现在,当 UITextField 发出 EditingDidEnd 时事件时,数据源将更新以包含用户输入的值。

希望这对您有所帮助!

关于c# - TableView 单元格在滚动时重置其数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31406317/

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