Domanda

I've got a TableView with custom cells, in the cells there is a label and a textfield. I want to focus the textfield when the row is selected.

Anyone knows how to fix this in this code:

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    //EDIT TEXTFIELD FROM SELECTED ROW
}

My textfield is indeed in a custom cell class. I tried this:

CustomCell cell = tableView.CellAt (indexPath) as CustomCell;
cell.textField.BecomeFirstResponder();

But the textField is never found.

This is my CustomCell class:

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Forms.iOS
{
public class CustomCell : UITableViewCell
{

    UITextField textField;
    UILabel label;

    public CustomCell (NSString cellId) : base (UITableViewCellStyle.Value1, cellId)
    {
        textField = new UITextField ();
        label = new UILabel ();
        ContentView.Add (label);
        ContentView.Add (textField);
    }

    public void UpdateCell (string textFieldValue, string labelValue)
    {
        DetailTextLabel.Text = "Dit is echt nutteloze tekst maar geen idee waarvoor ik dit hier nu neer zet maar zodat het in ieder geval te veel is.";
        textField.Placeholder = textFieldValue;
        TextLabel.Text = labelValue;
    }

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();

        DetailTextLabel.Hidden = true;
        RectangleF detailFrame = DetailTextLabel.Frame;
        textField.Frame = detailFrame;
    }

}
}
È stato utile?

Soluzione

I believe you would need to make the text field the first responder when selecting the row.

yourUITextField.BecomeFirstResponder();

If your UITextField is a member of some form of custom cell class you could try grab the cell at that location using CellAt, casting it to the custom class and accessing it that way.

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    CustomUITableViewCellClass customCell = tableView.CellAt(indexPath) as CustomUITableViewCellClass;

    if (customCell !=null)
    {
        customCell.yourUITextField.BecomeFirstResponder();
    }
    else
    {
        // Cell at indexPath cannot be cast to CustomUITableViewCellClass
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top