简体   繁体   English

tableview上的textfield.text显示值null

[英]textfield.text on tableview show value null

I use the codes below to add textfield to each tableview cell, 我使用以下代码向每个tableview单元格添加文本字段,

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSInteger row=[indexPath row];
    static NSString *SimpleTableIdentifier1 = @"CellTableIdentifier";
    //if I change the code to [NSString *SimpleTableIdentifier1 =NSString stringWithFormat:@"CellTableIdentifier%d",row]; everything is fine         


    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];

    if  (cell == nil){

        CGRect cellframe=CGRectMake(0, 0, 200, 60);
        cell=[[[UITableViewCell alloc] initWithFrame: cellframe  reuseIdentifier:SimpleTableIdentifier1] autorelease];
        UITextField * textfieldCell =[[UITextField alloc]init];
        textfieldCell.frame = CGRectMake(100.0f,20.0f,60.0f,26.0f) ;
        [textfieldCell setDelegate:self];
        [textfieldCell setTag:40000+row];//add row value here for later use, 
        [cell.contentView addSubview:textfieldCell];
        [textfieldCell release];

    }
    UITextField *textfieldCell ;

    textfieldCell =(UITextField*)[cell.contentView viewWithTag:40000+row];
    textfieldCell.text=[ NSMutableString  stringWithString:@"aaa1"];
    DebugLog(@"---------%@",textfieldCell.text);

    return cell;


}

textfieldCell.text sometimes displays null rather than my expectation value of 'aaa1' textfieldCell.text有时显示空值,而不是我的期望值'aaa1'

this means that the code line at: 这意味着代码行位于:

textfieldCell =(UITextField*)[cell.contentView viewWithTag:40000+row];

sometimes returns nil, try to fix this confused result but failed 有时返回nil,请尝试解决此混淆结果,但失败

Your comment welcome 欢迎您发表评论

This dequeueReusableCellWithIdentifier dequeueReusableCellWithIdentifier

 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];

returns a non nil cell Docs 返回非零单元格文档

This method dequeues an existing cell if one is available or creates a new one using the class or nib file you previously registered. 如果一个可用的单元格可用,则此方法使该单元出队,或者使用您先前注册的类或nib文件创建一个新的单元格。 If no cell is available for reuse and you did not register a class or nib file, this method returns nil 如果没有可供重用的单元格,并且您没有注册类或nib文件,则此方法返回nil

that may has a textField with different tag resulting in nil for 可能有一个带有不同标签的textField,结果为nil

 UITextField *textfieldCell ;

as these 2 lines 如这两行

// here rhs may be nil
textfieldCell =(UITextField*)[cell.contentView viewWithTag:40000+row];
textfieldCell.text=[ NSMutableString  stringWithString:@"aaa1"];

has no effect with nil textfield , also it's shocking how you still work with MRC ( Manual reference counting ) here 对nil文本字段没有影响,这也令人震惊,您仍然在这里使用MRC(手动引用计数)

[textfieldCell release];

please update to ARC ( Automatic reference counting ) which will removes your worries about memory management issues 请更新到ARC(自动引用计数),这将消除您对内存管理问题的担忧

UITableView uses a dynamic pool of cells for performance reasons. 出于性能原因,UITableView使用动态单元格池。 The only cells which are actually active are those which are currently displayed on the screen. 实际处于活动状态的唯一单元格是当前在屏幕上显示的单元格。 Any cells which are scrolled out of view are removed from the view and returned to a pool identified by, in your case, "SimpleTableIdentifier1" 滚动到视图之外的所有单元格都将从视图中删除,并返回到由“ SimpleTableIdentifier1”标识的池中

The method dequeueReusableCell retrieves a cell from the pool, if one is available, or creates one if a class or XIB has been registered, otherwise returns nil. 如果一个池可用,则方法dequeueReusableCell从池中检索一个池,如果已经注册了一个类或XIB,则创建一个池,否则返回nil。

The upshot of this is that your code may return a previously created cell which has been used for some other row earlier. 这样做的结果是您的代码可能返回先前创建的单元格,该单元格之前已用于其他某行。 The tag which exists for that cell will most likely relate to some other row. 该单元格存在的标签很可能与其他行相关。 This will all depend on the scrolling of rows on and off screen. 这都取决于屏幕上和行外的行滚动。

Changing the identifier to "CellTableIdentifier"%row causes each cell to be allocated from its own pool and avoids this conflict. 将标识符更改为“ CellTableIdentifier”%row会导致从其自己的池中分配每个单元,并避免了这种冲突。 However that will completely defeat the purpose of reusing cells. 但是,这将完全破坏重用细胞的目的。

Since you are using tag just to locate the text field within the cell you do not need to make the tag unique across all cells of the table. 由于您仅使用标记来查找单元格中的文本字段,因此无需使标记在表的所有单元格中唯一。 Just use a simple constant ie. 只需使用一个简单的常量即。 4000 not 4000 + row. 4000不是4000 +行。 viewWithTag will return the view within the hierarchy which matches, and you are starting at the content view anyway. viewWithTag将返回匹配的层次结构中的视图,无论如何,您都是从内容视图开始的。

Having said all that, the structure of your code is really not ideal. 说了这么多,您的代码结构实际上并不理想。 The better approach is to define your cell as a class, the layout in the storyboard, and access the text field directly as a member of the class. 更好的方法是将您的单元格定义为一个类,在情节提要中进行布局,并作为该类的成员直接访问文本字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM