0

Hi I have this problem that if I add more than 13 items in my uitableview the items will repeat

this is my code for

Adding:

[categoryList insertObject:inputcategory atIndex:0];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtindexPaths@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];

CellForRow:

if (cell == nil){
 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];
}

NumberofRows

return [categoryList count];

the first 12 items are okay but after that it adds the same last object in my NSMutableArray again in my uitableview and when I reload it, It only records 12 items.

3 Answers 3

4

In cellForRowAtIndexPath: method change

 if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];
 }

to

 if (cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
 }
 cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];

If you set text in if (cell == nil) it will not updated on reusing the cell. When reusing the cell will not be nil.

2
  • WOW Thanks! btw what's the explanation of this?
    – RonPelayo
    Commented Mar 18, 2014 at 3:17
  • @RonPelayo the values are repeated because the tableview reuses the cells. So when reusing cell will not be nil and here the content will not get updated and seems like repeating.
    – Akhilrajtr
    Commented Mar 18, 2014 at 3:21
1

Akhilrajtr's answer is correct but is the old way of doing it. In 2014 for iOS 6+ you should instead be using the UITableView methods

- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier

or

- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier

and

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath

Getting a cell in cellForRowAtIndexPath: becomes much simpler...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

"identifier" is the unique string used to identify cells for reuse, just like the old way.

From Apple's documentation, "This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered." You don't need to use initWithStyle any more, and if you think you do then consider subclassing UITableViewCell instead to more precisely control the appearance of your cells.

No more checks for the cell being nil are required. Just go ahead and set labels if your cell is a plain UITableViewCell or configure your subclassed cell (which should of course have made itself ready for reuse with the prepareForReuse: method)

0

Apple mention the performance this in their documentation on UITableViews.

it's should be about Reuse Cells. So if the cell != nil u can't got this below work:

 if (cell == nil){
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     cell.textlabel.text = [categoryList objectAtIndex:indexPath.row];  
}

Reuse cells. - Object allocation has a performance cost, especially if the allocation has to happen repeatedly over a short period—say, when the user scrolls a table view. If you reuse cells instead of allocating new ones, you greatly enhance table view performance.

1
  • @RonPelayo add this method In front of the code: 'UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];' 'dequeueReusableCellWithIdentifier' statement reuse cell.
    – MackC
    Commented Mar 19, 2014 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.