-
-
Save abrahamvegh/946828 to your computer and use it in GitHub Desktop.
Trying to figure out how I’m doing cell recycling wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
// Including the .h and .m file together for simplicity’s sake. | |
// yes, this is a horrific abuse of ObjC | |
@interface CSTextCell : UITableViewCell { | |
} | |
+ (id)generate:(UITableView *)tableView at:(NSIndexPath *)indexPath text:(NSString *)text; | |
+ (NSString *)cellID; | |
+ (id)cellForTableView:(UITableView *)tableView; | |
- (id)initWithStyle:(UITableViewCellStyle)style cellID:(NSString *)kCellID; | |
@end | |
@implementation CSTextCell | |
+ (id)generate:(UITableView *)tableView at:(NSIndexPath *)indexPath text:(NSString *)text { | |
NSString *kCellID = [NSString stringWithFormat:@"%@,%d,%d", [CSTextCell cellID], [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]]; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; | |
if(cell == nil) { | |
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease]; | |
} | |
cell.textLabel.text = nil; | |
if(![text isKindOfClass:[NSNull class]]) { | |
cell.textLabel.text = text; | |
} | |
return cell; | |
} | |
+ (NSString *)cellID { | |
return NSStringFromClass([self class]); | |
} | |
+ (id)cellForTableView:(UITableView *)tableView { | |
NSString *kCellID = [self cellID]; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; | |
if(cell == nil) { | |
cell = [[[self alloc] initWithStyle:UITableViewCellStyleDefault cellID:kCellID] autorelease]; | |
} | |
return cell; | |
} | |
- (id)initWithStyle:(UITableViewCellStyle)style cellID:(NSString *)kCellID { | |
self = [super initWithStyle:style reuseIdentifier:kCellID]; | |
if(self) { | |
// … | |
} | |
return self; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
// retrieve item name from the appropriately–filtered content array | |
NSString *itemName; | |
if(tableView == self.searchDisplayController.searchResultsTableView) { | |
itemName = [[self.filteredData objectAtIndex:indexPath.row] description]; | |
} else { | |
itemName = [[self.sectionedData objectAtIndexPath:indexPath] description]; | |
} | |
UITableViewCell *cell = [CSTextCell generate:tableView at:indexPath text:itemName]; | |
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment