RonPelayo
RonPelayo

Reputation: 376

Duplicate Object when adding in NSMutableArray [iOS]

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.

Upvotes: 0

Views: 192

Answers (3)

user189804
user189804

Reputation:

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)

Upvotes: 1

MackC
MackC

Reputation: 350

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.

Upvotes: 0

Akhilrajtr
Akhilrajtr

Reputation: 5182

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.

Upvotes: 4

Related Questions