我已经创建了一个动态表,但是当我尝试访问第一个单元格中文本字段的内容时遇到了问题。我得到的文本字段总是空的,即使我在调用操作之前在其中输入了一些文本。
这是我的代码。你能帮我找出问题所在吗?
自定义名称单元格.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/