您的位置:首页 > 编程语言 > Go语言

TTTableView自定义Section的例子 转自google论坛

2012-05-17 16:04 423 查看
IFor some weird Google bug I was not able to reply to the pertinent

thread, so I though I would just start a new one with this working

code. I am still not convinced this is the best way to do this, but

it works.

#import "Three20/TTTableViewDelegate.h"

#import "Three20/TTTableHeaderView.h"

@implementation TTTableViewDelegate (SectionTitlesCategory)

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:

(NSInteger)section {

if (tableView.style == UITableViewStylePlain && TTSTYLEVAR

(tableHeaderTintColor)) {

if ([tableView.dataSource respondsToSelector:@selector

(tableView:titleForHeaderInSection:)]) {

NSString* title = [tableView.dataSource tableView:tableView

titleForHeaderInSection:section];

if (title.length) {

TTTableHeaderView* header = [_headers objectForKey:title];

if (!header) {

if (!_headers) {

_headers = [[NSMutableDictionary alloc] init];

}

header = [[[TTTableHeaderView alloc] initWithTitle:title]

autorelease];

[_headers setObject:header forKey:title];

}

return header;

}

}

} else if (tableView.style == UITableViewStyleGrouped) {

if ([tableView.dataSource respondsToSelector:@selector(sections)]) {

NSString* title = [[tableView.dataSource performSelector:@selector

(sections)] objectAtIndex:section];

if (title.length) {

TTTableHeaderView *header = [_headers objectForKey:title];

if (!header) {

if (!_headers) {

_headers = [[NSMutableDictionary alloc] init];

}

header = [[[TTTableHeaderView alloc] initWithTitle:title]

autorelease];

[header

setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

[_headers setObject:header forKey:title];

}

return header;

}

}

}

return nil;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:

(NSInteger)section {

NSString* title = [[tableView.dataSource performSelector:@selector

(sections)] objectAtIndex:section];

if (title.length) {

return 30;

} else {

return 0;

}

}

@end

#import "Three20/TTTableHeaderView.h"

@implementation TTTableHeaderView (SectionTitlesCategory)

- (id)initWithTitle:(NSString*)title {

if (self = [super init]) {

self.backgroundColor = [UIColor clearColor];

self.style = TTSTYLE(groupedTableSectionHeader);

_label = [[UILabel alloc] init];

_label.text = title;

_label.backgroundColor = [UIColor clearColor];

_label.textColor = TTSTYLEVAR(tableHeaderTextColor) ? TTSTYLEVAR

(tableHeaderTextColor) : TTSTYLEVAR(linkTextColor);

_label.shadowColor = TTSTYLEVAR(tableHeaderShadowColor) ? TTSTYLEVAR

(tableHeaderShadowColor) : [UIColor clearColor];

_label.shadowOffset = CGSizeMake(0, 1);

_label.font = FONT_GROUPED_SECTION_HEADER_FONT;

[self addSubview:_label];

}

return self;

}

- (void)layoutSubviews {

_label.frame = CGRectMake(kDefaultMargin + kRoundedCellMargin, 0,

self.width, self.height);

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: