【问题标题】:Textfield in tableview cell changes text value表格视图单元格中的文本字段更改文本值
【发布时间】:2015-08-25 18:42:42
【问题描述】:

我遇到的问题是,当我滚动表格视图时,如果文本字段为空白(并且显示占位符值),则行显示相应的占位符值就好了。但是,如果我开始浏览表格视图并在文本字段中输入文本,一旦我向下滚动,之前的文本字段值就会开始显示在表格视图下方的其他文本字段中。我认为它与单元格标识符有关,但它似乎适用于我的标签和 uitextfield 占位符,而不是文本字段用户文本输入。有什么想法吗?这是代码...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.headerLabel.text = @"some string";
    cell.textField.placeholder = @"some other string placeholder,";
    cell.textField.delegate = self;

    return cell;
}

这里是 CustomCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        headerLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        headerLabel.translatesAutoresizingMaskIntoConstraints = NO;
        headerLabel.textColor = [UIColor colorWithRed:155.0/255.0 green:155.0/255.0 blue:155.0/255.0 alpha:1.0];
        headerLabel.font = [UIFont fontWithName:@"Helvetica Neue Reg" size:15];

        textField = [[UITextField alloc] initWithFrame:CGRectZero];
        textField.textColor = [UIColor colorWithRed:13.0/255.0 green:122/255.0 blue:255.0/255.0 alpha:1.0];
        textField.translatesAutoresizingMaskIntoConstraints = NO;
        textField.font = [UIFont fontWithName:@"Helvetica Neue" size:15];

        [self.contentView addSubview:headerLabel];
        [self.contentView addSubview:textField];

        [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:headerLabel
                                              attribute:NSLayoutAttributeLeading
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:self.contentView
                                              attribute:NSLayoutAttributeLeading
                                             multiplier:1
                                               constant:27 ] ] ;

        [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:headerLabel
                                              attribute:NSLayoutAttributeTop
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:self.contentView
                                              attribute:NSLayoutAttributeTop
                                             multiplier:1
                                               constant:10 ] ] ;

        [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:textField
                                              attribute:NSLayoutAttributeLeading
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:self.contentView
                                              attribute:NSLayoutAttributeLeading
                                             multiplier:1
                                               constant:27 ] ] ;

        [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:textField
                                              attribute:NSLayoutAttributeTop
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:headerLabel
                                              attribute:NSLayoutAttributeBottom
                                             multiplier:1
                                               constant:5 ] ] ;

        [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem:textField
                                              attribute:NSLayoutAttributeTrailing
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:self.contentView
                                              attribute:NSLayoutAttributeTrailing
                                             multiplier:1
                                               constant:-27 ] ] ;

    }

    return self;
}

【问题讨论】:

  • 您需要在文本更改时保存文本,并且您需要在cellForRowAtIndexPath: 方法中设置每个文本字段的text 属性。

标签: ios objective-c uitableview


【解决方案1】:

您是否为表格视图提供支持数据源?你遇到这个问题的原因是因为

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

此方法会重用您的单元格,并且由于您没有将文本与索引路径相关联,因此单元格会被重用,但会将您之前添加到其中的文本保留在其标签中。

所以这里是你可以解决它的方法。假设您在文本字段中输入了一些文本。您需要存储此数据(我通常使用数组)并使其与修改文本字段的单元格的单元格 indexPath.row 属性相对应。然后表格视图使用此数据源填充单元格中的标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.headerLabel.text = @"some string";

    //Using your data source array to get your data for each cell
    cell.textField.text = self.myDataSourceTextArray[indexPath.row];
    cell.textField.delegate = self;

    return cell;
}

较难的部分是将文本字段与某个索引路径相关联。我会让各个单元格成为文本字段的代表,然后让单元格自己在更新其文本字段时更新您的数据源并传递它们各自的索引路径,以便您可以保留正确单元格的文本。

【讨论】:

    【解决方案2】:

    在我的情况下,我想使用显示在同一单元格上的按钮操作来减少 tableview 中的文本字段值

    -(void)decreaseCount:(id)sender
    {
        UIButton *btn = (UIButton *)sender;
    
        UITableViewCell *cell = (UITableViewCell *) [[btn superview]superview];
    
        NSIndexPath *path = [tableView indexPathForCell:cell];
    
        UITextField *tempText = (UITextField *)[cell viewWithTag:10];
    
        if ([tempText.text integerValue] >0) {
            tempText.text = [NSString stringWithFormat:@"%ld",[tempText.text integerValue]-1];
    
        }
    
        // in my case I want to update Array
        [self updateArray: btn.tag :tempText.text];
    
    }
    
    // here i have replace the array object as per my need
    -(void)updateArray:(int) index :(NSString *)str
    {
        NSMutableArray * arrayCount = [[NSMutableArray alloc]init];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setValue:str forKey:@"count"];
        [arrayCount replaceObjectAtIndex:index withObject:dict];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多