0

バックグラウンド

  • UITableCellViewサブビューとして追加したカスタムUITextFieldを使用している(つまり、通常のUITableCellViewビューを使用していない)カスタムがあります
  • シナリオでは、セルを押す=>画面にジャンプして値を変更します(pushViewController/を介してnavigationControl)。次に、変更後、[戻る]ボタンを押してUITableViewに戻ります。
  • viewDidAppearこのアプローチを使用すると、そのシナリオに対する特定のコールバックはありません。そのため、一般的な方法を使用してこれをトラップするアプローチをUITableViewController使用しました。変更を更新するために使用した手法は次のとおりです。

コード:

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

    [self.tableView reloadData];    
}

しかし、私が注意するのは、上記のコードは次のとおりです。

  1. の通常/既存のフィールドで機能します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 に答える