简体   繁体   中英

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

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 ?

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";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM