0

背景

  • 有一个自定义UITableCellView,我正在使用我作为子视图添加的自定义 UITextField(即不使用普通UITableCellView视图
  • 在场景中按下单元格 => 跳转到屏幕以修改值(通过pushViewController/ navigationControl)。然后在更改后按 BACK 按钮返回 UITableView
  • 使用这种方法没有针对该场景的特定回调,因此我一直在使用使用通用viewDidAppear方法捕获此问题的方法UITableViewController- 我用来更新更改的技术是:

代码:

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

    [self.tableView reloadData];    
}

但我注意到的是上面的代码:

  1. 适用于 a 中的正常/现有字段UITableViewCell
  2. 不适用于我自定义的自定义 textField 子视图UITableViewCell

问题 - 在这个用例中,如何让我的自定义字段UITableView在我做出更改后返回时更新/显示?

例如:

  • 我是否需要以某种方式将我的自定义字段/子视图设置为“需要更新”?
  • 我是否需要覆盖reloadData某处/以某种方式设置自定义字段?

编辑:添加一些代码:

代码来自cellForRowAtIndexPath

(a) 符合标准的代码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) 不适用于自定义单元格中的自定义字段的代码

    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;

自定义单元格代码

界面:

@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

关键方法:

+ (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;
}
4

1 回答 1

0

好吧,我确实通过在“viewDidAppear”中添加以下行来修复它

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

但是它并不能真正帮助我理解为什么我在之前使用 UITableViewCell 中的标准文本字段时不需要这一行...注意我通过引用有效地设置了单元格文本的值

于 2011-10-10T00:34:32.243 回答