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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值