UITextField值在滚动时消失并重新出现在其他UITableViewCell中

问题描述:

- (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]; 

name.text = [NSString stringWithFormat:@"%@",inventoryItems[indexPath.row][@"Inventory_Name"]]; 

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



cell.selectionStyle = UITableViewCellSelectionStyleNone; 
return cell; 

} 

我是Objective-C的初学者。我有一个名称为UITextField的定制UITableViewCellUITextField标记为4.每次滚动时,UITextField中的值消失并出现在不同的单元格中。我明白,这是因为dequeueReusableCellWithIdentifier,其中单元格被重用。我已经研究了一下,发现将UITextField s存储在一个数组中,并将它们重新加载到cellForRowAtIndexPath中可能是最好的方法,但我在实现时遇到了问题。一个例子是理想的,或者一个简单的方法来解决这个问题。谢谢。 enter image description hereUITextField值在滚动时消失并重新出现在其他UITableViewCell中

每次调用cellForRowAtIndexPath:方法时,都会设置name UILabel。

我想知道/从inventoryItems[indexPath.row][@"Inventory_Name"]的价值是不是你所期望的?

而不是做的:

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

尝试这样做:

NSLog(@"%@ SHOULD EQUAL %@", inventoryItems[indexPath.row][@"Inventory_Name"], name.text); 

,看看他们总是匹配?我怀疑你有一些隐藏在那里的“<null>”值。

+1

仅供参考 - 这是一个转贴问题。我刚关闭它。你可能想发布你的答案。 – rmaddy