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

Mar*_*rko 2 iphone objective-c xamarin.ios ios

我发誓我已经在整个谷歌数据库中搜索了一个可能的解决方案,但我仍然坚持这个问题:)

基本上,我在数据库表中有12行,我在其中生成一个带有4个UITextField的自定义UITableViewCell.表列看起来像这样(SQLITE)

EntryFieldID NUMBER
Description  TEXT
FieldType    TEXT
Run Code Online (Sandbox Code Playgroud)

现在我希望每个UITableViewCellTag都是EntryFieldID上面的,所以我可以很容易地再引用它来存储在另一个表中.那张桌子看起来像这样.

OrderID      NUMBER
EntryFieldID NUMBER
Value1       TEXT
Value2       TEXT
Value3       TEXT
Value4       TEXT
Run Code Online (Sandbox Code Playgroud)

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

希望到目前为止这是有道理的:)

现在在我的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;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码对我来说实际上没有多大意义,它是从MonoTouch示例复制的.我知道单元格被重用但我实际上想要避免这种情况,所以我可以使用EntryFieldID作为自定义单元格的Tag属性,以便我可以循环遍历UITableView的所有行(我现在理解这可能是不可能的) .

所以,如果有人能够阐明我目前的做法,是否可行,或者我是否应该考虑重新实施整个事情.

mig*_*aza 7

如果您不认为UITableViewCells是用于保存状态的应用程序的构建块,那么您将自己做一件大事,但它们是什么:根据呈现信息的需要创建和销毁的瞬态对象.

如果你在思考UITableViewCell方面实现了这一飞跃,你会发现,无论在任何特定控件或数据结构的变化方面发生什么,都需要反映在其他地方.

您对代码的第一个问题是您按照预期的方式使用单元格:通过重用队列中的现有单元格,然后您确定这不是您想要的并且您正在尝试使用不同的查找系统.这就是为什么你的方法不起作用的原因.

你有几个选择:

(a)按照应该使用的方式使用UITableViewCells.这可能很烦人,但有一些工具可以使这更简单,像MonoTouch.Dialog或你可以检查我的博客条目uitableviewcells的设计模式:

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

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