文本字段中从最初的文本框自定义表视图细胞dissappears并在滚动

问题描述:

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

static NSString *CellIdentifier = @"inventoryItem"; 


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 



if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.textLabel.font = [UIFont systemFontOfSize:15]; 

} 
UILabel *name = (UILabel *)[cell viewWithTag:1]; 



NSLog(@"%@", name.text); 



cell.selectionStyle = UITableViewCellSelectionStyleNone; 
return cell; 

} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return inventoryItems.count; 
} 

我意识到这是与dequeueReuasableCellWithIdentifier做的,但我对如何解决这一不确定出现在其他细胞文本框。我在故事板上有文本框,然后我有一个操作来将所有这些文本框相对于它们的单元格发送到数据库。文本字段的代码位于IBAction中,用于发送数据并存储。我将添加IBAction为代码:文本字段中从最初的文本框自定义表视图细胞dissappears并在滚动

- (IBAction)submitCountTapped:(id)sender { 


NSMutableDictionary *dic; 



if(inventoryItems.count>0){ 
    NSMutableArray *prods = [[NSMutableArray alloc] init]; 

    for (int a=0; a<inventoryItems.count; a++) { 


     if([inventoryItems[a] objectForKey:@"InventoryID"]!=nil){ 


      UITableViewCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:a inSection:0]]; 


      UITextField *countTextfield = (UITextField *)[cell viewWithTag:4]; 

      double countValue = [countTextfield.text doubleValue]; 

      NSNumber *count = [NSNumber numberWithDouble:countValue]; 

      dic = [NSMutableDictionary dictionaryWithObjectsAndKeys: [self fetchDict:@"CustomerData"][@"User_ID"], @"User_ID", count, @"manual_quantity", inventoryItems[a][@"InventoryID"], @"InventoryID", nil]; 


      [prods addObject:dic]; 
     } 
    } 

    NSDictionary *outerDic = @{@"Data":prods}; 

    if([self isInternet]){ 

     [self fetchPostAddress:[NSString stringWithFormat:@"http://localhost/Backend/webservice/set-inventory-manually.php"] andParameter:[self jsonconvert:outerDic]andIdentifier:@"manualInventory"]; 


    } 
} 

}

+2

记得细胞被重用。因此,您应该保留(在字典中或使用自定义模型类)在textfielddidendediting时在文本字段中输入的文本。并且应该设置为cellForRowAtIndexPath函数中的单元格的文本字段 – jpulikkottil

+0

您能否提供一个示例? – user3773060

从创建自定义单元格自定义UITableViewCell类。而滚动

tableView: cellForRowAtIndexPath:

ConditionTableViewCell *objCell = (ConditionTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"conditionCell"]; 
objCell.countTextfield.text = @"Your text"; 
return objCell;