转载自:http://three20.pypt.lt/custom-cells-in-tttableviewcontroller
Custom cells in TTTableViewController
Implementation
Table view controller
HelloTableViewController.h
#import <Three20/Three20.h>
#import "HelloTableViewDataSource.h"
@interface HelloTableViewController : TTTableViewController {
}
@end
HelloTableViewController.m
#import "HelloTableViewController.h"
@implementation HelloTableViewController
- (id)init {
if (self = [super init]) {
self.title = @"Hello world table view";
self.variableHeightRows = YES;
}
return self;
}
- (void)createModel {
self.dataSource = [[[ArticleListDataSource alloc] init] autorelease];
}
@end
Data source for table view controller
HelloTableViewDataSource.h
#import "HelloTableItem.h"
#import "HelloTableItemCell.h"
@interface HelloTableViewDataSource : TTListDataSource {
}
@end
HelloTableViewDataSource.m
- (id)init {
if (self = [super init]) {
self.items = [NSArray arrayWithObjects:
[HelloTableItem itemWithTitle:@"First" subtitle:@"Hello #1!"],
[HelloTableItem itemWithTitle:@"Second" subtitle:@"Hello #2!"],
[HelloTableItem itemWithTitle:@"Third" subtitle:@"Hello #3!"],
[HelloTableItem itemWithTitle:@"Fourth" subtitle:@"Hello #4!"],
[HelloTableItem itemWithTitle:@"Fifth" subtitle:@"Hello #5!"],
nil];
}
return self;
}
- (Class)tableView:(UITableView*)tableView cellClassForObject:(id) object {
if ([object isKindOfClass:[HelloTableItem class]]) {
return [HelloTableItemCell class];
}
return [super tableView:tableView
cellClassForObject:object];
}
@end
Data item for a single cell
HelloTableItem.h
@interface HelloTableItem : TTTableLinkedItem {
NSString *_title;
NSString *_subtitle;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
+ (id)itemWithTitle:(NSString *)title
subtitle:(NSString *)subtitle;
@end
HelloTableItem.m
#import "HelloTableItem.h"
@implementation HelloTableItem
@synthesize title = _title, subtitle = _subtitle;
+ (id)itemWithTitle:(NSString *)title
subtitle:(NSString *)subtitle {
HelloTableItem *item = [[[self alloc] init] autorelease];
item.title = title;
item.subtitle = subtitle;
return item;
}
#pragma mark -
#pragma mark NSObject
- (id)init {
if (self = [super init]) {
_title = nil;
_subtitle = nil;
}
return self;
}
- (void)dealloc {
TT_RELEASE_SAFELY(_title);
TT_RELEASE_SAFELY(_subtitle);
[super dealloc];
}
#pragma mark -
#pragma mark NSCoding
- (id)initWithCoder:(NSCoder*)decoder {
if (self = [super initWithCoder:decoder]) {
self.title = [decoder decodeObjectForKey:@"title"];
self.subtitle = [decoder decodeObjectForKey:@"subtitle"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder*)encoder {
[super encodeWithCoder:encoder];
if (self.title) {
[encoder encodeObject:self.title
forKey:@"title"];
}
if (self.subtitle) {
[encoder encodeObject:self.subtitle
forKey:@"subtitle"];
}
}
@end
Custom cell for a table view
HelloTableItemCell.h
@interface HelloTableItemCell : TTTableLinkedItemCell {
UILabel *_titleLabel;
UILabel *_subtitleLabel;
}
@end
HelloTableItemCell.m
These NSString methods might be helpful here:
-
Single line
-
Multiple lines
#import "HelloTableItemCell.h"
@implementation HelloTableItemCell
+ (CGFloat)tableView:(UITableView*)tableView rowHeightForObject:(id)item {
// Set the height for the particular cell
return 85.0;
}
- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString*)identifier {
if (self = [super initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:identifier]) {
_item = nil;
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_titleLabel];
_subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_subtitleLabel];
}
return self;
}
- (void)dealloc {
TT_RELEASE_SAFELY(_titleLabel);
TT_RELEASE_SAFELY(_subtitleLabel);
[super dealloc];
}
#pragma mark -
#pragma mark UIView
- (void)layoutSubviews {
[super layoutSubviews];
// Set the size, font, foreground color, background color, ...
[_titleLabel setFrame:CGRectMake(...)];
// ...
[_subtitleLabel setFrame:CGRectMake(...)];
// ...
}
#pragma mark -
#pragma mark TTTableViewCell
- (id)object {
return _item;
}
- (void)setObject:(id)object {
if (_item != object) {
[super setObject:object];
HelloTableItem *item = object;
// Set the data in various UI elements
[_titleLabel setText:item.title];
[_subtitleLabel setText:item.subtitle];
}
}
@end