Question

I have 4 TextFields and I want to loop through them to check if they have a value. The TextFields are created in a GetCell method from my TableViewController.

public UITextField TextField;

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {

        if (elements [indexPath.Row].Type == "textField") {

            EditField element = elements [indexPath.Row] as EditField;

            NSString FieldID = new NSString ("EditField");

            UITableViewCell cell = tableView.DequeueReusableCell (FieldID);
            cell.SelectionStyle = UITableViewCellSelectionStyle.None;

            var setTextField = cell.ViewWithTag (99) as UITextField;

            if (setTextField == null) {
                TextField = new UITextField ();
                TextField.Placeholder = element.Placeholder;
                TextField.Tag = 99;
                TextField.SecureTextEntry = element.Secure;

                cell.AddSubview (TextField);

                EditFieldProperties ();
            }

            cell.TextLabel.Text = element.Label;
            cell.DetailTextLabel.Hidden = true;

            return cell;
        } 
    }

How can I loop all the TextFields to get all the values? I think I need to store them in an array or a dictionary but I don't know how to.

All I can get is the value of the last TextField with this code:

Console.WriteLine(TextField.Text);
Was it helpful?

Solution 3

I fixed it by adding a new List of UITextFields like this:

public List<UITextField> TextFieldList = new List<UITextField>();

And looping them like this:

foreach (UITextField item in TextFieldList) {
    if (item.Text == "") {
        item.BackgroundColor = UIColor.Red;
    } else {
        Console.WriteLine (item.Text);
    }
}

OTHER TIPS

What I suggest it's to create a list of textfields, so define the list some where in the code and init in the constructor

public List<UITextField> YourTextFields = new List<UITextField>();

public YouTableViewSourceConstructor()
{
    foreach(var elementItem in elements.Where(e => e.Type == "textField").ToList())
    {
        YourTextFields.Add(new UITextField(){Tag = 99});
    }
}

then in the GetCell method

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    //some your code

    if(cell.ViewWithTag (99) != null)
    {
        cell.RemoveSubview(cell.ViewWithTag (99));
    }

    var textField = YourTextFields [elements.Where(e => e.Type == "textField").ToList().IndexOf(elements [indexPath.Row])];
    cell.AddSubview (textField);

    //some your code
}

So in the YourTextFields you will have all your 4 textfields and your can easily access to them

Just make a separate class for cell and name it "CustomCell" and inherit it from UITableViewCell.
Make four text fields in its constructor and set the location of those text fields by overriding "LayoutSubview" function of UITableViewCell like below:

public class CustomCell : UITableViewCell
{

CustomCell
{

//Create text fields here
UITextView TextField1=new UITextView();
this.AddSubView(TextField1);

}

public override void LayoutSubviews ()
{

// set location of text fields in cell as below

 TextField1.Frame=new RectangleF( 32, 8, 60, 15);

}


}//end class

I hope this will definitely help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top