views:

40

answers:

3

Hi all,

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 ?

+1  A: 

Hello,

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];

a = [[UILabelView alloc] init];
a.tag = 10;
[cell.contentView addSubview:a];

}

UILabelView* aView = [cell.contentView viewWithTag:10];

aView.text=@"Some text";

[cell.contentView addSubview:a];

 return cell;

}

iPhoneDev
perfect . bull's eye. :)
hib
+1  A: 

The code above is in your cellForRowAtIndexPath call, right?

I am not sure if this was the result of a copy and paste malfunction, but you want to wrap up the "if (cell == nil)" block right after the initWithStyle call, in the code above you are missing the ending curly bracket.

BP
A: 

Looks like you configure cell only when you create it. But when you obtain reusable cell from

cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier2];

you do not change it - but you should, they are random: e.g. you want cell for row =1 and obtained reused cell from row = 3

btw: always better provide whole code - you should not force peole to guess what do you mean.

Vladimir