天天看點

tableView分組頭部标題處理

/**
 *  header标題
 */
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return self.hotComments.count ? @"最熱評論" : @"最新評論";
//    }
//
//    return @"最新評論";
//}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *header = [[UIView alloc] init];
//    header.backgroundColor = GlobalBgColor;
//    
//    UILabel *label = [[UILabel alloc] init];
//    label.textColor = RGBColor(67, 67, 67);
//    label.width = 200;
//    label.x = TopicCellMargin;
//    label.autoresizingMask = UIViewAutoresizingFlexibleHeight;
//    [header addSubview:label];
//    
//    // 設定文字
//    if (section == 0) {
//        label.text = self.hotComments.count ? @"最熱評論" : @"最新評論";
//    }else{
//        label.text = @"最新評論";
//    }
//    return header;
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *ID = @"header";
    // 先從緩存池中找header
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];

    UILabel *label = nil;
    if (header == nil) { // 緩存池中有,自己建立
        header = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:ID];
        header.contentView.backgroundColor = GlobalBgColor;

        label = [[UILabel alloc] init];
        label.textColor = RGBColor(, , );
        label.width = ;
        label.x = TopicCellMargin;
        label.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        label.tag = ;
        [header.contentView addSubview:label];
    }else{
        // 從緩沖池中取出來
        label = (UILabel *)[header viewWithTag:];
    }

    // 設定文字
    if (section == ) {
        label.text = self.hotComments.count ? @"最熱評論" : @"最新評論";
    }else{
        label.text = @"最新評論";
    }

    return header;
}