我创建了一个自定义 UITableViewCell,它有一个 UIlabel 和一个 UITextField。
在我的 firstViewController 中,我加载并显示自定义表格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
LoginTableCell *cell = (LoginTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"LoginTableCell" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (LoginTableCell *) currentObject;
break;
}
}
}
然后,当用户点击“登录”时,我想从那里获取 UITextField.text 值并将其存储到一个字符串中。
- (IBAction)login:(id)sender {
我知道这听起来很简单,但我一直坚持这一点,似乎无法找到解决问题的正确答案。
我以为我可以做这样的事情:
NSString *user = LoginTableCell.loginTextField.text;
但这会返回一个错误:
request for member 'text' in something not a structure or union
非常感谢任何帮助
谢谢
山姆
最佳答案
一般情况下你可以这样做。确保下面代码中的 indexPath 是具有所需文本字段的行的正确索引路径。
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
LoginTableCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *user = cell.loginTextField.text;
关于objective-c - 从 UITableView 获取 UITextField 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7090740/