ios - 具有文本字段删除值的自定义单元格中 dequeueReusableCellWithIdentifier 的 iPhone 问题

标签 ios uitableview

在 UITableView 上滚动时,我的单元格文本字段值出现问题。当我向下滚动并隐藏自定义单元格时,textField 的值将被删除。 dequeueReusableCellWithIdentifier 方法不起作用。我有这个:

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

 static NSString *SectionsTableIdentifier = @"MyCustomCell";

 MyCustomCell *cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    if (cell == nil) {
        NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil];
        cell = [objects objectAtIndex:0];
    }

 cell.labelCustomAttribute.text= @"Attribute Name";
 cell.textFieldCustomAttribute.delegate = self;

 return cell;


}

最佳答案

我发现在 viewDidLoad 方法中使用 tableView 注册自定义单元格更容易,然后只需使用 dequeueReusableCellWithIdentifier。如果您注册单元格,dequeue 方法将自动选择一个可重用单元格或分配一个新的自定义单元格(如果没有可用单元格)。

例子:

-(void)viewDidLoad
{
    [super viewDidLoad];

    // Get a point to the customized table view cell for MyCustomCell
    UINib *myCustomCellNib = [UINib nibWithNibName:@"MyCustomCell" bundle:nil];
    // Register the MyCustomCell with tableview
    [[self tableView] registerNib:myCustomCellNib forCellReuseIdentifier:@"MyCustomCell"];
}

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

    static NSString *SectionsTableIdentifier = @"MyCustomCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

    cell.labelCustomAttribute.text= @"Attribute Name";
    cell.textFieldCustomAttribute.delegate = self;

    return cell;

}

关于ios - 具有文本字段删除值的自定义单元格中 dequeueReusableCellWithIdentifier 的 iPhone 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691189/

相关文章:

ios - 在 Swift 中将参数传递给操作函数

ios - 首次启动后应用程序崩溃

ios - 选择行时 UITableView 崩溃

ios - 在 Swift 中创建特定的 JSON 结构

ios - 为什么使用 Apple 推送通知服务 SSL(沙盒)?

ios - 如何为forControlEvents 放置多个控制事件?

ios - 为什么 tableviewcell 返回一个可选的 ("")

ios - UITableView 1070 中的断言失败

iphone - 旋转拨盘 - 旋转限制

ios - UITableViewCell 的 textLabel 中的两行,新行仅由文本中的换行符确定