Cell和TextField在UITableView中不显示串行

时间:2015-04-21 07:25:35

标签: ios objective-c iphone uitableview

我使用的是UITextField,每行都有一个CellLable和TextField。 Lable和TextField数据来自数组。在运行应用程序时,所有数据都可以正常运行,但在滚动Tableview时,最后2-3行无组织。

NSMutableArray

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ArrFieldData= [NSMutableArray arrayWithObjects:@"Fist Name", @"Last Name",@"User Name", @"Password",@"Confirm Password", @"Gender",@"DOB", @"Profile Pic",@"Deparment", @"Joining Date",@"Education", @"Role", nil];

现在是cellForRowAtIndexPath函数

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


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UITextField *txtField ;
if (cell ==nil) {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
     txtField = [[UITextField alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3 + 40, 2, [UIScreen mainScreen].bounds.size.width/2, cell.layer.frame.size.height -5)];

    [self setUpCell:cell withIndexPath:indexPath withTextField:txtField];
}


    [self UpdateCell:cell withIndexPath:indexPath withTextField:txtField];

return cell;
}

SetUp Cell Function

-(void)setUpCell:(UITableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath withTextField: (UITextField *)txtField {

cell.textLabel.text = [ArrFieldData objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:12.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
txtField.tag = indexPath.row+1;
txtField.layer.borderColor = [UIColor grayColor].CGColor;
txtField.delegate = self;
txtField.layer.borderWidth = 1.0f;
txtField.layer.cornerRadius = 4.0f;
txtField.placeholder = cell.textLabel.text;
txtField.font = [UIFont systemFontOfSize:12.0];
[cell.contentView addSubview: txtField];
}

更新单元格功能

-(void)UpdateCell:(UITableViewCell *)cell withIndexPath:(NSIndexPath *)indexPath withTextField: (UITextField *)txtField {
    [self setUpCell:cell withIndexPath:indexPath withTextField:txtField];
}

首次运行应用程序,它显示所有单元格和文本字段数据都是Serialize。但滚动时,某些单元格和TextField不按照数组值进行序列化。我正在附加模拟器截图。

First Time Running enter image description here

首次运行时的第一个屏幕截图,以及滚动Tableview时的第二个屏幕截图。请参阅最后的4-5单元格和Textfield占位符文本。他们没有组织。我希望它不应该改变。

2 个答案:

答案 0 :(得分:1)

您的问题归因于UITableView的功能。当您滚动UITableView时,indexPath已更新,因此您没有从tableView获得期望的索引值。

而不是以编程方式添加UITextField。创建自定义UITableViewCell,并从方法cellForRowAtIndexPath:更新UITextField的placeHolder。 tableView将为您处理滚动。

使用此代码:

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


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UITextField *txtField ;
if (cell ==nil) {
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.textField.placeholder= cell.textLabel.text;
}


    cell.textField.placeholder= cell.textLabel.text;

return cell;
}

答案 1 :(得分:1)

当您使用可重复使用的单元格时,例如

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            UITextField *txtField ;

            if (cell ==nil) 
            {
                 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
                 txtField = [[UITextField alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3 + 40, 2, [UIScreen mainScreen].bounds.size.width/2, cell.layer.frame.size.height -5)];

                [self setUpCell:cell withIndexPath:indexPath withTextField:txtField];
            }

            [self UpdateCell:cell withIndexPath:indexPath withTextField:txtField];

            return cell;
     }

UITableView使用可重用单元的概念,通过减少内存消耗来实现最大性能,并利用重用单元的这一功能,您可以使用上述UITableView's API来实现这一目标。

但在使用任何功能之前,了解任何功能的工作和使用非常重要。

在上面的tableView: cellForRowAtIndexPath:方法实现中,您使用了单元可重用性的概念。

如果单元格不存在并且是第一次创建,则比分配它们(每个子视图都在单元格的内容视图中创建并添加),使用来自数据源的数据进行自定义和初始化。各自的索引路径。

但是如果单元格被重用(因为它们已经为任何其他索引路径创建),则存在子视图,其中已经为之前为其创建的索引路径填充了数据。

现在我们可以做两件事来为当前索引路径使用已创建的单元格

1)如果单元格包含带数据的子视图,则删除子视图并重新创建新视图,自定义并用数据填充它们。

2)不是发布以前的子视图并创建新的子视图,而是重新填充相应索引路径的数据模型的数据。

在您的情况下,如果正在为任何索引路径创建单元格,则还会创建为其提交的文本,如果重新使用它,则不会创建新文本字段并且正在重复使用从之前创建的单元格,因此占位符文本的问题与左侧文本不匹配。

因此,为了解决您的问题,我认为您应该在创建单元格时创建文本字段,以及是否重复使用单元格,而不是重新填充从数据源中提取的文本中的数据。相应的索引路径。