UITextFields in UITableView, entered values reappearing in other cells

后端 未结 2 667
借酒劲吻你
借酒劲吻你 2021-01-13 08:49

I have a UITableView with about 20 cells, in each cell there are three UITextFields. I did NOT subclass UITableViewCell, but when setting up each cell I set the textfields t

2条回答
  •  有刺的猬
    2021-01-13 09:14

    Just get the textfield text into Dictionary setObject by label text and again check by label Text assign the text to corresponding Textfield.. here is my code...

    //In Interface
    NSMutableDictionary *amounts;
    amounts =[[NSMutableDictionary alloc]init];
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"MoneyEntryIdentifier";
    static NSString *CellNib = @"MoneyEntry";
    MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (!cell)
    {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (MoneyEntryTableViewCell *)[nib objectAtIndex:0];
    }
    
    UILabel     *lblname  = (UILabel *)    [cell lblMemName];
    lblname.tag =100;
    UITextField *txtfield = (UITextField *)[cell textAmount];
    txtfield.tag =indexPath.row;
    
    [txtfield addTarget:self  action:@selector(textFieldDidChange:)    forControlEvents:UIControlEventEditingChanged];
    lblname.text = tabledata[indexPath.row];
    txtfield.placeholder = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    
    if ([amounts valueForKey:lblname.text] != nil) {
     txtfield.text = [amounts valueForKey:lblname.text];
    } else {
     txtfield.text = @"";
    }
    
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
    }
    
     -(void)textFieldDidChange:(UITextField *)txtField
     {
     UILabel *label = (UILabel *)[txtField.superview viewWithTag:100];
     NSString *labelString = label.text;
     NSString *textFieldString = txtField.text;
     [amounts setObject:textFieldString forKey:labelString];
     }
    

    Change the nib to your tableview cell...

提交回复
热议问题