Reputation: 398
I know there are bunch of questions here related to this topic but i couldn't find any that matches my issue. I have subclass of UITableViewCell
named "CustomTableViewCell".
The below mentioned code works fine however, i have check marks in my cell and i wanted to stop the reusability since the max number of cells will not exceed 25, hence it would no affect the performance, i guess after many searches, i found out that i have to either remove the tableView dequeueReusableCellWithIdentifier
method or just change the identifier to nil, but unfortunately, i get empty cell when i i do that, and my app crashes what am i missing here?
This works fine BUT it reuses cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
...
...
...
...
return cell;
}
While instead, this code crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
...
...
return cell;
}
Upvotes: 0
Views: 515
Reputation: 2083
I strongly recommend you to use the reuse pattern, in my opinion its just bad practice not to do it. I guess the real reason that you avoid to use the reuse pattern is that you see 'old' state in your custom cells when they are being reused.
To avoid this, you should implement/override the prepareForReuse method in your custom cell, in this method you set all your custom properties to their default state.
In the cellForRowAtIndexPath you just reuse the cells and set your custom properties to their values.
- (void)prepareForReuse() {
[super prepareForReuse];
//Set your custom properties to their default state
self.custom1 = ...;
self.custom2 = ...;
}
Upvotes: 2
Reputation: 9040
1) @property (nonatomic, strong) NSMutableDictionary *cellArray;
2)in viewDidLoad,
self.cellArray = [[NSMutableDictionary alloc] init];
3)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomCell";
CustomTableViewCell *cell = nil;
id c = [self.cellArray objectForKey:[NSNumber numberWithInt:indexPath.row]];
if(!c){
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = @"init part goes here";
...
...
...
...
[self.cellArray setObject:cell forKey:[NSNumber numberWithInt:indexPath.row]];
}else{
cell = (CustomTableViewCell *) c;
}
return cell;
}
Upvotes: 0
Reputation: 73688
Remove the who chunk of code which which deals with reusing cell
. No need to check if cell
is nil
. Everytime, create a new instance of UITableViewCell
.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
//do a bunch of stuff to fill the cell.
return cell;
}
This piece of code will create a new cell
everytime.
Upvotes: 0