【问题标题】:custom uitableviewcell with textfield disappearing文本字段消失的自定义 uitableviewcell
【发布时间】:2016-09-18 21:15:52
【问题描述】:

有根据用户输入添加到 tableview 的自定义文本字段单元格。当用户添加多个手机号码时,类似于通讯录。 Nib 使用以下代码注册到表格视图。

[self.tableView registerNib:[UINib nibWithNibName:@"TextFieldTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:textFieldTableViewCellIdentifier];

添加动态单元格的方法:

-(void)addAccoladesValue
{
    NSMutableArray *DataArray =  [self.tableData[SectionTagAccolades-1][sectionDataKey] mutableCopy];
    NSMutableArray *DataValueArray =  [self.tableData[SectionTagAccolades-1][sectionDataValue] mutableCopy];
    self.accoladesArray = self.accoladesArray ? self.accoladesArray : [NSMutableArray new];
    TextFieldTableViewCell *accoladesCell = [self setupTextField:self.accoladesArray tag:TextFieldTagFirstName placeholder:@"Description" cellTextFieldIdentifier:textFieldTableViewCellIdentifier];
    accoladesCell.hidden = NO;
    [DataArray addObject:accoladesCell];
    [DataValueArray addObject:@""];
    self.tableData[SectionTagAccolades-1][sectionDataKey] = DataArray;
    self.tableData[SectionTagAccolades-1][sectionDataValue] = DataValueArray;
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:SectionTagAccolades-1]] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:SectionTagAccolades-1]] withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView endUpdates];
}

代码似乎可以正常工作,但有时单元格会消失,并且有几次新添加的单元格会被添加到顶部,这是错误的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TextFieldTableViewCell *cell = (TextFieldTableViewCell*)self.tableData[indexPath.section][sectionDataKey][indexPath.row];
    if ([cell isKindOfClass:[TextFieldTableViewCell class]])
    {
        cell.textfield.tag = indexPath.row;
        cell.textfield.superview.tag = indexPath.section;
        cell.textfield.text = self.tableData[indexPath.section][sectionDataValue][indexPath.row];

    }
    cell.hidden = NO;
    [cell setNeedsUpdateConstraints];
    [cell setNeedsLayout];
    return cell;
}

这种奇怪行为的可能原因是什么?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    我实际上可以在这里发现很多可能出错的地方。几件事。

    1。不要存储TableViewCells

    您应该手动存储cellsUITableView 会为您处理这些。 UITableView 将重用滚动到视图之外的cells 以显示新数据,因此可以与不同的objects 关联。

    tableView:cellForRowAtIndexPath: 中,在tableView 上调用dequeueReusableCellWithIdentifier:forIndexPath: 以获取一个单元格,然后用它应该显示的数据填充它。

    2。数据结构

    看起来您的self.accoladesArray 包含应该在TextFieldTableViewCells 中显示的数据。你为什么一直引用这个,一个instance property和一个self.tableData

    如果您只保留实例属性,您的 UITableViewDataSource 方法将归结为

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       // accolades section
       return self.accoladesArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       // accolades section
       TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: textFieldTableViewCellIdentifier forIndexPath: indexPath];
       NSString * text = self.accoladesArray[indexPath.row];
       // custom setup
       return cell;
    }
    
    - (void) addAccoladesValue
    {
        if (!self.accoladesArray) self.accoladesArray = [NSMutableArray new]; 
        [self.tableView beginUpdates];
        {
            [self.accoladesArray insertObject: @"" atIndex: 0];
            [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:SectionTagAccolades-1]] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        [self.tableView endUpdates];        
    }
    

    【讨论】:

    • 感谢 Aerows。我遵循了错误的策略来实现这一点。我已经按照建议更新了代码,它的工作流程更少。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多