Table Cell SubView Iteration Not Finding TextField

前端 未结 4 459
面向向阳花
面向向阳花 2021-01-17 07:03

I\'ve created a table where each cell holds both a text label and a text field. I\'m adding the textfields as such [cell addSubview:passwordField]; and from a v

相关标签:
4条回答
  • 2021-01-17 07:28

    Why not have a datasource mapped to the TableView and just retrieve / update the values in the datasource. You can then call reloadRowsAtIndexPaths to load just the row you just changed. Trying to iterate through the TableView rather than just updating the datasource seems very inefficient.

    0 讨论(0)
  • 2021-01-17 07:44

    Instead of UIView* subView = [[cell.contentView subviews]lastObject]; you can try to find it as:

    for(UIView *view in [cell subviews])
    {
      if([view isKindOfClass:[UITextfield class]]){
        // view is the reference to your textfield
      }
    }
    

    That way you can add other UIViews as subviews and still get the reference of the textfield without having to keep track of its subview index.

    0 讨论(0)
  • 2021-01-17 07:48

    You have added your textField on subView of cell.

    [cell addSubview:passwordField];
    

    While you're trying to find it on cell.contentView.
    Add your textField as a subView of cell.Contentview

    [cell.contentView addSubview:passwordField];
    

    And find it in this way -

    for(UIView *view in [cell.contentView subviews])
    {
        if([view isKindOfClass:[UITextfield class]])
        {
           UITextField *textField = (UITextField *)view;
           NSLog(@"%@",textField.text);
        }
    }
    
    0 讨论(0)
  • 2021-01-17 07:52

    2 things occur to me:

    1. In the long run it'll be easier to create a UITableViewCell which contains a UITextField which is accessible as a property. You can either use a nib to layout the cell or do it programmatically in the cells init method. This approach will make your code easier to manage.

    2. You need to consider cell reuse. If you are reusing cells (which you should be) then you will need store the fetch the value from the textfield before it is reused.

    0 讨论(0)
提交回复
热议问题