简体   繁体   中英

how to trigger update of a custom UITableViewCell after returning to view via back button?

Background

  • have a custom UITableCellView in which I'm using a custom UITextField that I've added as a subview (ie not using normal UITableCellView views
  • in the scenario is pressing on the cell => jump to screen to modify value (via pushViewController / navigationControl ). Then after changing hitting the BACK button to go back to the UITableView
  • using this approach there is no specific call back for that scenario, so I've been using the approach where you trap this using the general viewDidAppear method of the UITableViewController - the technique I'd used to update the change was:

Code:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.myNormalCell.textLabel.textColor = myColor;

    [self.tableView reloadData];    
}

But what I note is that the above code:

  1. works for for normal/existing fields in a UITableViewCell
  2. does NOT work for my custom textField subviews I've put in my custom UITableViewCell

QUESTION - How to, in this use case, get my custom fields to be udpated/shown on the UITableView when I come back to it after making a change?

For example:

  • do I need to somehow set my custom field/subview as "needs to be updated"?
  • do I need to override reloadData somewhere/somehow to set the custom field?

EDIT: Add some code:

CODE FROM cellForRowAtIndexPath

(a) Code that works with standard UITableViewCell

    self.lookAheadDaysCell = (ConfigCell*)[tableView dequeueReusableCellWithIdentifier:@"LookAheadWeeks"];
    if (self.lookAheadDaysCell == nil) {
        self.lookAheadDaysCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"LookAheadWeeks"] autorelease];
        self.lookAheadDaysCell.textLabel.text = @"      Weeks into Future";
        self.lookAheadDaysCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    self.lookAheadDaysCell.detailTextLabel.text = [self.weConfig.lookAheadWeeks stringValue];
        return self.lookAheadDaysCell;

(b) Code that doesn't work with custom field in custom cell

    self.lookAheadDaysCell = [ConfigCell cellForReuseId:@"LookAheadWeeks" 
                                      TableView:tableView 
                                          Title:@"Number of Weeks" 
                                       ShowHelp:true 
                                            Tag:9999 
                                       Delegate:self 
                                   UseTextField:false
                                    ContentText:[self.weConfig.lookAheadWeeks stringValue]];
    return self.lookAheadDaysCell;

CODE FROM CUSTOM CELL

Interface:

@interface ConfigCell : UITableViewCell <UITextFieldDelegate> {
    UITextField *_textField;
    UILabel *_titleLabel;
}
@property (nonatomic, retain) UITextField *textField;
@property (nonatomic, retain) UILabel *titleLabel;
+ (ConfigCell*) cellForReuseId:(NSString *)reuseId TableView:(UITableView*)tableView Title:(NSString*)titleStr ShowHelp:(BOOL)showHelp  Tag:(NSInteger)tag Delegate:(id)delegate  UseTextField:(BOOL)useTextField ContentText:(NSString*)text;
@end

Key Methods:

+ (ConfigCell*) cellForReuseId:(NSString *)reuseId TableView:(UITableView*)tableView Title:(NSString*)titleStr ShowHelp:(BOOL)showHelp Tag:(NSInteger)tag Delegate:(id)delegate UseTextField:(BOOL)useTextField ContentText:(NSString*)text
{
    // Get Cell (via reuse or create a new one)
    ConfigCell *cell = (ConfigCell*)[tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        // Create Cell
        cell = [[[ConfigCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        // View 1 - Title Label
        <<cut>>

        // View 2 - TextField for entry
        cell.textField = [[[UITextField alloc] initWithFrame:CGRectMake(0,0,0,0)] autorelease];
        cell.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        cell.textField.font = [UIFont systemFontOfSize:16];
        cell.textField.textAlignment = UITextAlignmentLeft;
        cell.textField.text = text;
        if (useTextField) {
            cell.textField.delegate = delegate;
            cell.textField.tag = tag;
            cell.textField.returnKeyType = UIReturnKeyDone;
        } else {
            cell.textField.userInteractionEnabled = false;
        }
        [cell.contentView addSubview:cell.textField];

    }
    return cell;
}

well I did fix it by putting the following line in "viewDidAppear"

self.lookAheadDaysCell.textField.text = [self.weConfig.lookAheadWeeks stringValue];

however it doesn't really help me understand why I didn't need this line when I was using the standard text fields in a UITableViewCell before...noting I'm effectively setting the value of the cell text by reference

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