UITextFields在UITableView中,输入的值重新出现在其他单元格中 [英] UITextFields in UITableView, entered values reappearing in other cells

查看:100
本文介绍了UITextFields在UITableView中,输入的值重新出现在其他单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView大约20个单元格,在每个单元格有三个UITextFields。我没有子类UITableViewCell,但是当设置每个单元格时,我将textfields标记值设置为常数加上行号。



当我运行应用程序并在例如第一行输入一个值时,它可能会重新出现在第12行。每次运行应用程序时,行为不一样。



我应该补充一点,我有一个存储每个文本字段中输入的内容的数组。当编辑一个文本字段时,我将新值保存到数组,当tableView再次请求单元格时,我将textfields的值设置为存储在数组中的值。



这与重用UITableViewCells有关吗?
当重用单元格时,不同行中的文本字段可以获得相同的标签编号?例如,第一个单元格(textfield tag = 1001)在第12行重复使用,则我们有两个文本字段具有相同的标签号。如果我然后在第1行和稍后加载第12行输入一个值,那么单元格1中的值将从数组中加载并放入第12行。



正在发生,我如何解决它?
每个单元格都没有对文本字段的引用,所以我不认为我可以通过访问它所在的单元格来编辑文本字段的标签值。



编辑:



下面是UITableView的代码:cellForRowAtIndexPath:

 code> UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//修改单元格,添加具有行唯一索引值的textfield
cell = [self modifyCellForHoleInfo:cell atIndexPath:indexPath];

}
cell.accessoryType = UITableViewCellAccessoryNone;

//加载dataArray中存储的textfield的值
((UITextField *)[cell viewWithTag:1000 + indexPath.row])text = [dataArray objectAtIndex:indexPath.row];


$ b

谢谢!

解决方案

问题是,当单元格被重用(即 dequeueReusableCellWithIdentifier 返回非nil)时,单元格返回现有的 UITextView 。为了保持标签的唯一性,最好删除任何以前的 UITextField

   - (void)removeExistingTextSubviews:(UITableViewCell *)cell 
{
NSMutableArray * toRemove = [NSMutableArray array];
//我不知道你是否有非TextField子视图
([cell subviews]中的id视图){
if([view isKindOfClass:[UITextField class]]& & view.tag> = 1000){
[toRemove insert:view];
}
}

for(在toRemove中的id视图){
[toRemove removeFromSuperView];
}
}

...

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
[self removeExistingTextSubviews:cell];
}
//修改单元格,添加具有行唯一索引值的textfield
cell = [self modifyCellForHoleInfo:cell atIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;

//加载dataArray中存储的textfield的值
((UITextField *)[cell viewWithTag:1000 + indexPath.row])text = [dataArray objectAtIndex:indexPath.row];

请注意,我没有编译代码,但它应该作为起点。 / p>

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 tag value to a constant plus the row number. So for each row the tag of the textfield is incremented by one.

When I run the app and enter a value in for example row one, it may reappear on row 12. The behavior is not the same each time i run the app.

I should add that I have an array storing the contents entered in each textfield. When a textfield is edited, I save the new value to the array and when the cell is again requested by the tableview i set the textfields value to that stored in the array.

Does this have something to do with reusing UITableViewCells? When reusing a cell, can textfields in different rows get the same tag numbers? Say for example that the first cell (textfield tag=1001) is reused on row 12, then we have two textfields with the same tag number. If I then enter a value on row 1 and later load row 12, the value from cell one will be loaded from the array and put in row 12 also.

If this is happening, how do I fix it? Each cell does not have a reference to the textfield, so I don't think I can edit a textfield's tag value by just having access to the cell which it is in.

EDIT:

Here is the code for UITableView:cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            //Modify cell, adding textfield with row-unique index value
            cell = [self modifyCellForHoleInfo:cell atIndexPath:indexPath];

    }
    cell.accessoryType = UITableViewCellAccessoryNone; 

    // Load value for textfield stored in dataArray
    ((UITextField *)[cell viewWithTag:1000+indexPath.row]).text = [dataArray objectAtIndex:indexPath.row];

Thanks!

解决方案

The issue is that when the cell is reused (i.e. dequeueReusableCellWithIdentifier returns non-nil), the cell is returned with the existing UITextView. To maintain the uniqueness of the tags, it'll be better to remove any previous UITextField:

- (void)removeExistingTextSubviews:(UITableViewCell *)cell
{
    NSMutableArray *toRemove = [NSMutableArray array];
    // I don't know if you have non-TextField subviews
    for (id view in [cell subviews]) {
        if ([view isKindOfClass:[UITextField class]] && view.tag >= 1000) {
           [toRemove insert:view];
        }
    }

    for (id view in toRemove) {
        [toRemove removeFromSuperView];
    }
}

...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
    [self removeExistingTextSubviews:cell];
}
//Modify cell, adding textfield with row-unique index value
cell = [self modifyCellForHoleInfo:cell atIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone; 

// Load value for textfield stored in dataArray
((UITextField *)[cell viewWithTag:1000+indexPath.row]).text = [dataArray objectAtIndex:indexPath.row];

Please note that I haven't compiled the code, but it should serve as a starting point.

这篇关于UITextFields在UITableView中,输入的值重新出现在其他单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆