Question

I am working on an iOS application where I have a UITableView that contains custom UITableViewCell's. Each cell in the table contains a UITextField that receives numeric input. Underneath the UITableView is another view that contains buttons, and one of these buttons needs to be disabled until all of the UITextFields in the UITableView have been filled. Once all of the UITextFields are full, then and only then does the button become enabled. How do I do this? I have the following relevant code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ...
    
    static NSString *cellIdentifier = @"Cell";
    
    _cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (_cell == nil) {
        
        _cell = [[SimpleTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    _cell.dataField.tag = indexPath.row;
    _cell.dataField.delegate = self;
    _cell.dataField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [_cell.dataField setTextAlignment:NSTextAlignmentRight];
    [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
    return _cell;
    
}

I am implementing the UITextFieldDelegate and I figure my solution will have to work with the method:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {

}

However, I'm not sure how to enable a particular button ONLY when all UITextFields in the table have data in them. How can I do this?

Update

I have added the following method into my code:

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    for (int i = 0; i < [self.table numberOfSections]; i++) {
        
        NSInteger rows =  [self.table numberOfRowsInSection:i];
        
        for (int row = 0; row < rows; row++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
            SimpleTableCell *cell = (SimpleTableCell *)[self.table cellForRowAtIndexPath:indexPath];
            
            for (UIView *subView in cell.subviews) {
                
                if ([subView isKindOfClass:[UITextField class]]) {
                    //NEVER REACHES THIS POINT
                    UITextField *txtField = (UITextField *)subView;
                    
                    if([[txtField text] length] > 0) {
                        
                        //Enable button and bold title
                        [_doneButton setEnabled:YES];
                        [_doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:28]];
                        
                    }
                    
                }
                
            }
            
        }
        
    }
    
    return YES;
    
}

While this method is being called, the problem is that it is not ever reaching inside the if statement:

if ([subView isKindOfClass:[UITextField class]]) {...}

even though when I put a breakpoint here, I see in the variable subViewsCache that one of the subViews is indeed of type UITextField. I set the enabled attribute of the button to "NO" in the viewDidLoad method, but unfortunately, it remains in this state. What am I doing wrong?

Was it helpful?

Solution

May be this Bellow Method you got all textfiled of UITableview and Set you logic as par you need put Bellow Method in to textFieldShouldEndEditing Hope this Helps you

for (int i = 0; i < [self.tableView numberOfSections]; i++)
    {
        NSInteger rows =  [self.tableView numberOfRowsInSection:i];
        for (int row = 0; row < rows; row++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:i];
            UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
            for (UIView *subView in cell.subviews)
            {
                if ([subView isKindOfClass:[UITextField class]])
                {
                    UITextField *txtField = (UITextField *)subView;
                    if(txtField.text.length>0)
                    {
                        //found value in textfiled

                    }
                    else
                    {
                       //Emplty textfiled
                    }

                }
            }

        }

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