- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
-----编辑-----
通过在 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 中消失的单元格都将重置为默认值。
我不希望此 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/
我正在尝试使用谷歌浏览器的 Trace Event Profiling Tool分析我正在运行的 Node.js 应用程序。选择点样本后,我可以在三种 View 之间进行选择: 自上而下(树) 自上而
对于一个可能是菜鸟的问题,我们深表歉意,但尽管在 SO 上研究了大量教程和其他问题,但仍找不到答案。 我想做的很简单:显示一个包含大量数据库存储字符串的 Android ListView。我所说的“很
我已经开始了一个新元素的工作,并决定给 Foundation 5 一个 bash,看看它是什么样的。在创建带有水平字段的表单时,我在文档中注意到的第一件事是它们使用大量 div 来设置样式。所以我在下
我有一个 Windows 窗体用户控件,其中包含一个使用 BeginInvoke 委托(delegate)调用从单独线程更新的第 3 方图像显示控件。 在繁重的 CPU 负载下,UI 会锁定。当我附加
我有一堆严重依赖dom元素的JS代码。我目前使用的测试解决方案依赖于 Selenium ,但 AFAIK 无法正确评估 js 错误(addScript 错误不会导致您的测试失败,而 getEval 会
我正在制作一款基于滚动 2D map /图 block 的游戏。每个图 block (存储为图 block [21][11] - 每个 map 总共 231 个图 block )最多可以包含 21 个
考虑到以下情况,我是前端初学者: 某个 HTML 页面应该包含一个沉重的图像(例如 - 动画 gif),但我不想强制客户缓慢地等待它完全下载才能享受一个漂亮的页面,而是我更愿意给他看一个轻量级图像(例
我正在设计一个小软件,其中包括: 在互联网上获取资源, 一些用户交互(资源的快速编辑), 一些处理。 我想使用许多资源(它们都列在列表中)来这样做。每个都独立于其他。由于编辑部分很累,我想让用户(可能
我想比较两个理论场景。为了问题的目的,我简化了案例。但基本上它是您典型的生产者消费者场景。 (我关注的是消费者)。 我有一个很大的Queue dataQueue我必须将其传输给多个客户端。 那么让我们
我有一个二元分类问题,标签 0 和 1(少数)存在巨大不平衡。由于测试集带有标签 1 的行太少,因此我将训练测试设置为至少 70-30 或 60-40,因此仍然有重要的观察结果。由于我没有过多地衡量准
我是一名优秀的程序员,十分优秀!