gpt4 book ai didi

iphone - iOS 无法在 UITableViewCell 中设置 UITextField 文本属性

转载 作者:行者123 更新时间:2023-11-29 03:59:48 26 4
gpt4 key购买 nike

我的 iOS 应用程序有一个“people”的 UITableView。我创建了一个“添加”屏幕来将新人员添加到我的持久存储中,效果非常好。

此添加 View 使用使用 UITableView 和子类 UITableViewCell 创建的表单,以及 UITextFieldUILabel >,虽然效果很好,但我对 iOS 编程很陌生,觉得这可能不是最有效的方法。

我正在尝试重新使用此添加 View 作为我的详细信息、添加和编辑 View ,并且我可以成功地将“人员”实体设置为详细信息 View 中的属性。我的 viewDidLoad 方法中有以下代码,其中 adding 属性是在上一个 ViewController 的 prepareForSegue 方法中设置的:

if (self.adding)
{
self.editing = YES;
self.title = @"Add Person";
}
else
{
self.title = self.person.name;

[self setDetail];
}

我的问题是,当我尝试预填充详细信息 View 的字段(我的 setDetail 方法)时,我无法使用 person 实体中的 name 属性设置 UITextField 文本。这是我用来检索 UITextField 并设置其 text 属性的代码:

form 是 UITableView;

UITableViewCell *nameCell  = [form cellForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
UITextField *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;

如果我 NSLog nameCell 它返回 (null)

我希望这已经足够解释了。任何指针都会有很大帮助!

最佳答案

不要在 viewDidLoad: 中设置,而是在 TableView 数据源方法中进行设置。即

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Deque the nameCell and prepare the cell if its not available.
UITextField *nameField = (UITextField *)[nameCell viewWithTag:100];
nameField.text = self.person.name;
return nameCell.
}

关于iphone - iOS 无法在 UITableViewCell 中设置 UITextField 文本属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16025434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com