objective-c - 从动态单元格中的文本字段获取数据时出现问题

标签 objective-c ios xcode uitableview

我已经创建了一个动态表,但是当我尝试访问第一个单元格中文本字段的内容时遇到了问题。我得到的文本字段总是空的,即使我在调用操作之前在其中输入了一些文本。

这是我的代码。你能帮我找出问题所在吗?

自定义名称单元格.h

@interface CustomNameCell : UITableViewCell
    @property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@end

CustomViewController.h

@interface CustomViewController : UITableViewController
    @property (retain, nonatomic) IBOutlet UITableView *tableview;
    - (IBAction)search:(id)sender;
@end

自定义 View Controller .m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        static NSString *CellIdentifier = @"nameCell";
        CustomNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) {
            cell = [[CustomNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        return cell;
    }
    ...

以及单击按钮时的操作,我在其中尝试获取文本字段的内容:

- (IBAction)search:(id)sender {

static NSString *CellIdentifier = @"nameCell";
CustomNameCell *nameCell = [_tableview dequeueReusableCellWithIdentifier:CellIdentifier];

here nameCell.nameTextField.text is always equal to @""

为什么我的文本框总是空的?

谢谢你的帮助;)

最佳答案

如果你需要一个单元格,你应该这样得到它:

CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:indexPath];
NSString * textInField = cell.nameTextField.text;

如果您告诉我您需要什么逻辑,我会扩展我的问题。现在 - 您可以使用此方法。

第一个单元格的内容将是:

CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSString * textInField = cell.nameTextField.text;

关于objective-c - 从动态单元格中的文本字段获取数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13740757/

相关文章:

ios - UILocalNotification具有重复一次的行为,例如在Messages App中

iphone - phonegap 是否支持离线存储缓存 list ?

iphone - 如何在 Xcode 中添加两个编译器标志

iphone - 在不重复使用标识符的情况下使用多个自定义单元格

ios - 使用 Core Graphics 时为 "Potential leak of an object"

ios - iPad 上的 UIDatePicker 布局问题

iphone - iPhone 5横向模式下,导航栏右侧的按钮停止工作,如何解决?

swift - Xcode 9.0 版自动建议的默认 Swift 版本是什么?

ios - 将某些 XIB/Storyboard 字符串标记为不可本地化

ios - 如何将过滤器应用于以前录制的声音并使用 AudioKit 保存修改后的版本?