Reputation: 749
I placed three textfields in one row and 1 textfield in another row. I took all the textfields in one array and wrote the following in 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;
}
and in the textFieldDidEndEditing
method wrote as follows.
[textFieldsDataArray replaceObjectAtIndex:currentIndexPath.row withObject:textField.text];
My Problem is once i enter text in one textfield and scroll up and down then that same text is taking to remaining two textfields in that same row.
and if i enter text in three textfields and then scroll , that is taking text from the last textfield for all.
Upvotes: 0
Views: 480
Reputation: 7484
Sounds like you are having an issue with cell reuse.
To increase performance, UITableView will reuse a cell off screen instead of creating a new cell. When a cell gets reused, it does not clear it out. Its your job to reassign what information you want in it.
you set the information something like this:
- (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;
}
Upvotes: 1