我在一行中放置了三个文本字段,在另一行放置了一个文本字段。我将所有文本字段放在一个数组中,并在cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ];
if (indexPath.row == 0) {
UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
tf.delegate = self;
tf.tag = 16;
tf.borderStyle = UITextBorderStyleRoundedRect;
[cell.contentView addSubview:tf];
[tf release];
UITextField *tf1 = [[UITextField alloc]initWithFrame:CGRectMake(150, 10, 100, 30)];
tf1.delegate = self;
tf1.tag = 17;
tf1.borderStyle = UITextBorderStyleRoundedRect;
[cell.contentView addSubview:tf1];
[tf1 release];
}
else {
UITextField *tf3 = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
tf3.delegate = self;
tf3.tag = 18;
tf3.borderStyle = UITextBorderStyleRoundedRect;
[cell.contentView addSubview:tf3];
[tf3 release];
}
}
UITextField *tf = (UITextField *)[cell.contentView viewWithTag:16];
tf.text = [m_textFieldArray objectAtIndex:indexPath.row];
UITextField *tf1 = (UITextField *)[cell.contentView viewWithTag:17];
tf1.text = [m_textFieldArray objectAtIndex:indexPath.row];
UITextField *tf3 = (UITextField *)[cell.contentView viewWithTag:18];
tf3.text = [m_textFieldArray objectAtIndex:indexPath.row];
// cell.textLabel.text = @"Hiiiii";
return cell;
}
并在textFieldDidEndEditing
方法中写如下。
[textFieldsDataArray replaceObjectAtIndex:currentIndexPath.row withObject:textField.text];
我的问题是,一旦我在一个文本字段中输入文本并向上和向下滚动,然后相同的文本将占用同一行中剩余的两个文本字段。
如果我在三个文本字段中输入文本然后滚动,那就是从最后一个文本字段中获取文本。
答案 0 :(得分:1)
听起来您遇到了细胞重用问题。
为了提高性能,UITableView将重用屏幕上的单元格而不是创建新单元格。当一个单元被重用时,它不会清除它。您的工作是重新分配您想要的信息。
你设置的信息是这样的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]
// If the cell is nil, it is a new cell and you need to init it
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// setting the cell's information
cell.textLabel.text = [dataSource objectAtIndex:indexPath.row];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}