通过后退按钮返回视图后如何触发自定义 UITableViewCell 的更新?
背景
- 有一个自定义
UITableCellView
,其中我使用了一个已添加为子视图的自定义 UITextField (即, - 在场景中不使用正常的
UITableCellView
视图,按下cell => 跳转到屏幕来修改值(通过pushViewController
/navigationControl
),然后在更改后点击“BACK”按钮返回到 UITableView - 使用这种方法没有。具体调用回到那个场景,所以我一直在使用使用
UITableViewController
的通用viewDidAppear
方法来捕获它的方法 - 我用来更新更改的技术是:
代码:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.myNormalCell.textLabel.textColor = myColor;
[self.tableView reloadData];
}
但我注意到上面的代码:
- 适用于
UITableViewCell
中的普通/现有字段, - 不适用于我放入自定义
UITableViewCell 中的自定义 textField 子视图
问题 - 在此用例中,当我在进行更改后返回到 UITableView 时,如何在 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;
}
Background
- have a custom
UITableCellView
in which I'm using a custom UITextField that I've added as a subview (i.e. not using normalUITableCellView
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 theUITableViewController
- 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:
- works for for normal/existing fields in a
UITableViewCell
- 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我确实通过将以下行放入“viewDidAppear”中来修复它,
但是它并不能真正帮助我理解为什么当我之前在 UITableViewCell 中使用标准文本字段时不需要这一行......注意到我通过引用有效设置单元格文本的值
well I did fix it by putting the following line in "viewDidAppear"
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