iphone - 从自定义 UITableViewCell 中的 UITextFields 中检索值

标签 iphone objective-c ios xamarin.ios

我发誓我已经搜索了整个 Google 数据库以寻找可能的解决方案,但我仍然停留在这个问题上:)

基本上,我在数据库表中有 12 行,为此我生成了一个自定义 UITableViewCell,其中包含 4 个 UITextFields。表列看起来像这样(SQLITE)

EntryFieldID NUMBER
Description  TEXT
FieldType    TEXT

现在我希望每个 UITableViewCell 标记都是上面的 EntryFieldID,这样我以后可以很容易地引用它来存储在另一个表中。那张 table 看起来像这样。

OrderID      NUMBER
EntryFieldID NUMBER
Value1       TEXT
Value2       TEXT
Value3       TEXT
Value4       TEXT

4 个 Value* 字段是每个 UIViewCell 中的 4 个 UITextFields

希望到目前为止这是有意义的:)

现在在我的 TableViewSource 中我有这段代码:

protected List<InfoCaptureTableViewGroup> _tableItems;
protected string _customCellIdentifier = "InfoCaptureField";
protected Dictionary<int, InfoCaptureTableViewCell> _cellControllers = new Dictionary<int, InfoCaptureTableViewCell>();

public InfoCaptureTableSource (List<InfoCaptureTableViewGroup> items)
{
    this._tableItems = items;
}

/// <summary>
/// Called by the TableView to determine how many sections(groups) there are.
/// </summary>
public override int NumberOfSections (UITableView tableView)
{
    return this._tableItems.Count;
}

/// <summary>
/// Called by the TableView to determine how many cells to create for that particular section.
/// </summary>
public override int RowsInSection (UITableView tableview, int section)
{
    return this._tableItems[section].Items.Count;
}

/// <summary>
/// Called by the TableView to retrieve the header text for the particular section(group)
/// </summary>
public override string TitleForHeader (UITableView tableView, int section)
{
    return this._tableItems[section].Name;
}

/// <summary>
/// Called by the TableView to retrieve the footer text for the particular section(group)
/// </summary>
public override string TitleForFooter (UITableView tableView, int section)
{
    return this._tableItems[section].Footer;
}

/// <summary>
/// Called by the TableView to retreive the height of the row for the particular section and row
/// </summary>
public override float GetHeightForRow (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    return 44f;
}

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    // is there a way to NOT reuse cells? There's only ever going to be 12 of them
    UITableViewCell cell = tableView.DequeueReusableCell (this._customCellIdentifier);
    InfoCaptureTableViewCell customCellController = null;
    InfoCaptureField item = this._tableItems[indexPath.Section].Items[indexPath.Row];

    //---- if there are no cells to reuse, create a new one
    if (cell == null)
    {
        customCellController = new InfoCaptureTableViewCell();

        // retrieve the cell from our custom cell controller
        cell = customCellController.Cell;

        // disable selection
        cell.SelectionStyle = UITableViewCellSelectionStyle.None;

        // This is where I'd like to use the EntryFieldID
        cell.Tag = Environment.TickCount;

        // store our controller with the unique ID we gave our cell
        this._cellControllers.Add (cell.Tag, customCellController);
    }
    else
    {
        // retrieve our controller via it's unique ID
        customCellController = this._cellControllers[cell.Tag];
    }

    //---- return the custom cell
    return cell;
}

上面的代码对我来说实际上并没有太大意义,它是从一个 MonoTouch 示例中复制的。我知道单元格会被重用,但实际上我想避免这种情况,因此我可以将 EntryFieldID 用于自定义单元格的 Tag 属性,并且还可以循环遍历 UITableView 的所有行(我现在明白这可能是不可能的) .

那么,如果有人可以阐明我目前的方法,它是否可行,或者我是否应该考虑重新实现整个事情。

最佳答案

如果您不将 UITableViewCells 视为保持状态的应用程序构建 block ,而是考虑它们的本质:根据呈现信息的需要创建和销毁的 transient 对象,那么您将帮自己一个大忙。

如果您在思考 UITableViewCell 时实现了这一飞跃,您就会意识到,无论单元格发生什么变化,对任何特定控件或数据结构的更改,都需要在其他地方反射(reflect)出来。

您的代码遇到的第一个问题是您按照预期的方式使用单元格:通过重用队列中的现有单元格,但随后您决定这不是您想要的,并且您正在尝试使用不同的查找系统。这就是您的方法行不通的原因。

您有几个选择:

(a) 按照应有的方式使用 UITableViewCells。这可能很烦人,但有一些工具可以使这变得更简单,比如 MonoTouch.Dialog 或者您可以查看我关于 uitableviewcells 设计模式的博客条目:

http://tirania.org/monomac/archive/2011/Jan-18.html

(b) 您可以不在代码中重复使用相同的单元格标识符,而是为每一行使用一个单元格标识符。这将强制 UITableView 创建新的单元格,每行一个,您以后要查找。如果您只需要 12 行,这应该没问题。

关于iphone - 从自定义 UITableViewCell 中的 UITextFields 中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4801065/

相关文章:

ios - 从 Gmail 帐户中获取所有电子邮件

objective-c - 检查应用程序是否安装在/Application目录内

objective-c - 检测 UITableView 中的水平平移

objective-c - 在接口(interface)文件中导入类和前向声明(@class)有什么区别?

iphone - iPhone-在核心数据上获取数据

iphone - cocos2d 和 box2d : sprite touch not get properly

iPhone - 在 UIImagePickerController 上显示模态视图

ios - 发现意外的 Mach-O header 代码 : 0x72613c21 while generating ipa file

ios - UITableView 在 "pull down to refresh"期间顶部有额外空间

iphone - @2x 和图像分辨率的混淆