I am using table view for editing an object .
I am using below code to construct a uitableview cell.
It is working nicely but somehow while scrolling the cells changing internally .
static NSString *CellIdentifier2 = @"CustomCell Editable Identifier";
cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier2] autorelease];
cell.textLabel.text = rowLabel;
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 10, 170, 25)];
theTextField.textAlignment = UITextAlignmentRight;
cell.editingAccessoryView =theTextField;
theTextField.tag = 602;
if ([rowKey isEqualToString:@"phone_no"]) {
[theTextField setKeyboardType:UIKeyboardTypeNumberPad];
}
theTextField.text = [rowValue ddEventValueDisplay];
[theTextField release];
return cell;
Can anyone tell me where am I wrong ?
Your are creating a label when cell is NIL. So every time you scroll, cellForRowAtIndexPath get called and it check whether the cell is NIL (which is not this time) so nothing get executed which is in if(cell==NILL) block.
So to set the label text you need to grape out this label from cell view and then set the text as:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabelView* aView = [cell.contentView viewWithTag:10];
aView.text=@”Some text”;
[cell.contentView addSubview:a];
}