开发者

TTTableViewController with custom cell sample

开发者 https://www.devze.com 2023-03-15 05:30 出处:网络
I am h开发者_如何学编程aving difficulties in using TTTableViewController with a custom cell? In order to use a custom cell then can I use a UITableViewCell or do I have to subclass TTTableViewCell? Ho

I am h开发者_如何学编程aving difficulties in using TTTableViewController with a custom cell? In order to use a custom cell then can I use a UITableViewCell or do I have to subclass TTTableViewCell? How do I use TTModel? Is there any good examples out there that shows all of this?


The biggest issue with TTTableViewCell is that it works completely different from the standard UITableViewCell & UITableViewDelegate, so you will have to forget what you know about UITableViewCell.

In nutshell, TTTableItem classes are data containers, while TTTableItemCell classes are responsible for displaying the cells in the table.

For example, TTTableMessageItem class contains all the data about a message, such as date, title and body. The TTTableMessageItemCell class contains all the UI elements for the cell and their layout.

I found it to be more flexible than the standard UITableViewController, because you can easily change cell types.

If you want to add your own custom class type, you will need to define both a new TTTableItem subclass and new TTTableItemCell subclass.

HelloTableItem.h - contains the data for the cell:

@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

HelloTableItemCell.h - the cell itself

@interface HelloTableItemCell : TTTableLinkedItemCell {
    UILabel *_titleLabel;
    UILabel *_subtitleLabel;
}

@end

HelloTableViewDataSource.m - The datasource is responsible to add the table items and to decide which type of cell to use for each item type

- (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

The complete source code can be found here: http://three20.pypt.lt/custom-cells-in-tttableviewcontroller

0

精彩评论

暂无评论...
验证码 换一张
取 消