Skip to content

Instantly share code, notes, and snippets.

@asmallteapot
Created April 28, 2011 17:27
Show Gist options
  • Save asmallteapot/946813 to your computer and use it in GitHub Desktop.
Save asmallteapot/946813 to your computer and use it in GitHub Desktop.
Trying to figure out how I’m doing cell recycling wrong.
#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];
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
- (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