开发者

UITableViewCell dequeueReusableCellWithIdentifier is not in order

I have a UITable with few custom cells which contains text fields.开发者_Go百科 However when up scroll up and down the the order of those cells are messed up.

I searched and this is not working for me cos I have few UITextFields where I need to grab user inputs for those.

UITableViewCell showing indexpath rows out of order

Thank You


You either need to use a unique identifier for each of the cells, or else avoid using the dequeuing mechanism altogether. If you're doing the latter, store a reference to each cell in a corresponding instance variable, or else add them all to a collection stored in an instance variable.

If you decide to go this route, you could do this one of two ways: either create the cells in Interface Builder (which would be handy, because they could already have the text fields nested inside them, nicely positioned, etc.) and connect them using outlets, or create the cells programmatically in viewDidLoad or in an init... method.

In either case, you would no longer need to call dequeueReusableCellWithIdentifier:; instead you could just return the cell instances you've already created.


@Dilshan you can do something similar to this in your code to achieve what you want.

.
.
.
if(cell == nil)
{
// create your cell in this block of quotes in the cellForRowAtIndexPath: method to avoid making of the cells again, so that your textfield value remain as it is.
.
.
.
//place all the code in this block only.
}
[cell autorelease];
return cell;

sorry i don't have xcode at home so i can't give you any specific code.

but i think you get the idea .
cheers.


Here is my code. CustomTextFieldCell is a cell with a UITextField. and some info. However the populated value and the order of those changes when you scroll.

    static NSString *CellIdentifier = @"CustomTextFieldCell";
    CustomTextFieldCell *cell = (CustomTextFieldCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomTextFieldCell" owner:nil options:nil];


        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (CustomTextFieldCell *) currentObject;
                break;
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜