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

Objective c 从动态单元格中的文本字段获取数据时出现问题,objective-c,ios,xcode,uitableview,Objective C,Ios,Xcode,Uitableview,我已经创建了一个动态表,但是当我试图访问第一个单元格中文本字段的内容时,遇到了一个问题。我得到的文本字段总是空的,即使我在调用操作之前在其中输入了一些文本 这是我的密码。你能帮我找出它的毛病吗 CustomNameCell.h @interface CustomNameCell : UITableViewCell @property (retain, nonatomic) IBOutlet UITextField *nameTextField; @end CustomViewContr

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

这是我的密码。你能帮我找出它的毛病吗

CustomNameCell.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
CustomViewController.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;

您不应该调用
[\u tableview dequeueReusableCellWithIdentifier:CellIdentifier]
在-
(iAction)搜索中:(id)sender
,因为它是在创建tableView期间重用单元格的一种机制。滚动tableview时,您没有创建新的单元格,而是重用了以前创建的一些单元格

底线:使用
cellForRowAtIndexPath
来访问分区中特定行的单元格,如果您想要第一个分区的第一个单元格,它将是:

CustomNameCell * cell = (CustomNameCell *)[tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

我就是这样做的。我不知道这是目前的做法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return cellContentArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId=@"cell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    UITextField *firsttext=[cell viewWithTag:101];
    UITextField *second=[cell viewWithTag:102];
    [second setTag:(indexPath.row)+1000];
    [firsttext setTag:(indexPath.row)+100];
    return cell;
}
- (IBAction)buttonPressed:(id)sender 
{
    for (int i=0; i<cellContentArray.count; i++) {
        UITableViewCell * cell = (UITableViewCell *)[baseTableveiew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        UITextField *tempTextField=[cell viewWithTag:i+100];
        UITextField *secondTextField=[cell  viewWithTag:i+1000];
        NSString * textInField = tempTextField.text;
        NSLog(@"tempTExtTiedl tag %ld data %@",(long)tempTextField.tag, textInField);
        NSLog(@"second tag %ld data %@",(long)secondTextField.tag, secondTextField.text);
    }

}
-(NSInteger)节数表视图:(UITableView*)表视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回cellContentArray.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellId=@“cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellId];
}
UITextField*firsttext=[cell viewWithTag:101];
UITextField*second=[带标记的单元格视图:102];
[第二个setTag:(indexath.row)+1000];
[firsttext-setTag:(indexPath.row)+100];
返回单元;
}
-(iAction)按钮按下:(id)发件人
{

对于(int i=0;i您不应在搜索方法中调用_tableviewdequeuereusableCellWithIdentifier:CellIdentifier您如何存储正在表中显示的数据?我的tableview包含动态但也包含“静态”单元格。我调用此方法获取特定单元格,第一个单元格包含搜索文本字段。单击按钮时,搜索文本字段用于填充表中其他单元格的日期。这不是您应该执行的方式。您是否可以指定您需要的内容?我无法使用单元格标识符获取特定单元格?我只是想知道第一个单元格中nameTextField.text的内容…@Nanego当你调用dequeueReusableCellWithIdentifier:CellIdentifier这只是一种机制,可以重用你在构建TableView时创建的单元格谢谢你的帮助。我本来希望使用identifier而不是索引,但它与索引配合得很好。此外,这个帮助感谢您的帮助。我曾希望使用标识符而不是索引,但效果很好。