UITableView with embeded UITextFields, using section footer for validation, update footer issues?

146 Views Asked by At

I'm using a UITableView with two UITextFields on on each row, to set a passcode and the confirm passcode.

I'd like to use the section footer to show if the passcodes match or if too short etc.

I'm using..

[[self passcodeTableView]reloadSections:[NSIndexSet indexSetWithIndex:0] 
                withRowAnimation:UITableViewRowAnimationFade];

To refresh the footer text, along with an NSString I set elsewhere.

However when I call reloadSections it forces the UITextFields to be re-added in cellForRowAtIndexPath.

I thought if (cell == nil) would stop the cells from being redrawn, but obviously not.

What am I doing wrong here ?

2

There are 2 best solutions below

0
J. Costa On BEST ANSWER

Create a variable to store the text and be accessible within your object. Add this to your YourClass.m:

@interface YourClass () {   
     UILabel *footerLabel;
}

@end

Add this method to your class:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (!footerLabel) {
        footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.0)];
    }

    return footerLabel;
}

- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 60.0;
}

Then, when you want to change the text, just update the footerLabel:

footerLabel.text = @"Your new text";
0
Hossam Ghareeb On

You can add these textFields as global variables and be created once, then you pass them to cellForRowAtIndexPath. They will be added with their previous state