First and last UITableViewCell keep changing while scrolling?

As you guessed, the issue is most likely coming from the conventional reuse of cells in a UITableView. Create an NSMutableDictionary as a property of your class, and whenever a UITextField is finished editing, set the value of it to a key in your dictionary.

Up vote 0 down vote favorite share g+ share fb share tw.

I have a tableView with cells containing one UITextField as a subview for each cell. My problem is that when I scroll down, the text in the first cell is duplicated in the last cell. I can't for the life if me figure out why.

I have tried loading the cells from different nibs, having the textFields as ivars. The UITextFields don't seem to be the problem, I'm thinking it has something to do with the tableView reusing the cells. The textFields all have a data source that keeps track of the text within the textField and the text is reset each time the cell is shown.

Any ideas? I'd really appreciate some more answers for this question. UPDATE 2: This is the code I have for a custom cell, called JournalCell.

Really appreciate the feedback. I have 8 sections with 1 row each. The first 7 have a textField in them, the last is a cell acting like a button.

I'm testing for the button cell, if it matches the section (7), then it returns that cell, if not, it continues to the rest. Could this be it? - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"Section %i, Row %i", indexPath.

Section, indexPath. Row); if (indexPath. Section == 7) { static NSString *ButtonCellIdentifier = @"ButtonCellIdentifier"; UITableViewCell *buttonCell = self.

TableView dequeueReusableCellWithIdentifier:ButtonCellIdentifier; if (buttonCell == nil) { buttonCell = UITableViewCell alloc initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ButtonCellIdentifier autorelease; buttonCell. SelectionStyle = UITableViewCellSelectionStyleBlue; buttonCell. AccessoryType = UITableViewCellAccessoryNone; buttonCell.textLabel.

Text = sClearAll; buttonCell.textLabel. TextAlignment = UITextAlignmentCenter; buttonCell. BackgroundColor = UIColor colorWithWhite:1.0 alpha:0.8; buttonCell.textLabel.

BackgroundColor = UIColor clearColor; } return buttonCell; } static NSString *TextCellIdentifier = @"JournalCellIdentifier"; JournalCell *cell = (JournalCell *)self. TableView dequeueReusableCellWithIdentifier:TextCellIdentifier; if (cell == nil) { NSBundle mainBundle loadNibNamed:@"JournalCell" owner:self options:nil; cell = customCell; cell. SelectionStyle = UITableViewCellSelectionStyleNone; cell.

AccessoryType = UITableViewCellAccessoryNone; cell.textField. AutocapitalizationType = UITextAutocapitalizationTypeWords; cell.textField. ReturnKeyType = UIReturnKeyNext; cell.textField.

ClearButtonMode = UITextFieldViewModeWhileEditing; } switch (indexPath. Section) { case 0: switch (indexPath. Row) { case 0: cell.textField.

AutocapitalizationType = UITextAutocapitalizationTypeNone; self. AuthorTextField = cell. TextField; self.authorTextField.

Text = self. TextFieldDictionary objectForKey:@"author"; NSLog(@"Reading Author:%@", self. TextFieldDictionary objectForKey:@"author"); break; } break; case 1: switch (indexPath.

Row) { case 0: self. YearTextField = cell. TextField; self.yearTextField.

Text = self. TextFieldDictionary objectForKey:@"year"; NSLog(@"Reading Year:%@", self. TextFieldDictionary objectForKey:@"year"); break; } break; case 2: switch (indexPath.

Row) { case 0: self. VolumeTextField = cell. TextField; self.volumeTextField.

Text = self. TextFieldDictionary objectForKey:@"volume"; NSLog(@"Reading Volume:%@", self. TextFieldDictionary objectForKey:@"volume"); break; } break; case 3: switch (indexPath.

Row) { case 0: self. ArticleTextField = cell. TextField; self.

ArticleTextField. Text = self. TextFieldDictionary objectForKey:@"article"; NSLog(@"Reading Article:%@", self.

TextFieldDictionary objectForKey:@"article"); break; } break; default: break; } return cell; } iphone cocoa-touch uitableview ios uitableviewcell link|improve this question edited Jan 17 '11 at 3:46 asked Jan 8 '11 at 20:02W Dyson1,9881723 82% accept rate.

1 provide some sample code so that we can locate the problem ;) – relikd Jan 8 '11 at 20:06 you have some problems in reusing cells properly. Post your cellForRowAtIndexPath: method so we can help to fix it – Vladimir Jan 8 '11 at 20:15 I updated the sample code to reflect what I'm using for a Custom Cell. – W Dyson Jan 9 '11 at 3:29 Anyone have feedback on the new code I've posted?

– W Dyson Jan 15 '11 at 1:12.

As you guessed, the issue is most likely coming from the conventional reuse of cells in a UITableView. Create an NSMutableDictionary as a property of your class, and whenever a UITextField is finished editing, set the value of it to a key in your dictionary. Then, in your cellForRowAtIndexPath method, load the value of the corresponding key in the dictionary.

It would be most ideal to name each key in your dictionary with the format indexPath section, indexPath row, as this will aid in the setting and getting of values. Hope this helps.

Canada Dev's answer will probably solve your issue, but it's only really a stop-gap rather than a long-term repair. You should avoid adding subviews in the cellForRowAtIndexPath: method. You should instead create a custom UITableViewCell (typically by subclassing UITableViewCell) and using that - otherwise you're adding/removing subviews from cells whenever they are dequeued.

If you're not sure what dequeuing of cells actually means, you should probably read up Apple's TableView documentation. Basically, use a UITableViewCell subclass, and you'll be in a much better position than trying to add all your views in your cellForRow method.

I was using a custom cell with a textField property. It was doing the same thing. Basically, I just want a UITextField in each cell that I can access through UITextFieldDelegate.

If anyone can post an outline of some sample code, I'd greatly appreciate that. – W Dyson Jan 8 '11 at 22:06.

Maybe you can save the whole cell in a property (e.g. Self. AuthorCell) and then simply display this cell: if (indexPath. Section == 0) { return self.

AuthorCell; // or cell = self. AuthorCell } and you can still access the textField by accessing the cell property: self.authorCell. MyTextField // EDIT: see the Apple documentation for "The Technique for Static Row Content.

This might work. I'll try it later. I was trying to set the cell as the one I wanted to return.

I now see my mistake. – W Dyson Jan 11 '11 at 22:32.

Try inserting this right after allocing the cell and before doing any drawing/labeling/etc. cell. ClearsContextBeforeDrawing = YES; cell.textLabel. Text = nil; cell.detailTextLabel.

Text = nil; for (UIView *view in cell.contentView. Subviews) { view removeFromSuperview; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions