自定義Section:修改UITableView的Section的背景和字體

第一次嘗試修改UITableView的Section的背景和字體,頭疼好一陣,終於找到了方法:
如同自定義Cell一樣,使用UITableView的函數,可以自定義Section:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;


例子代碼:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *sectionTitle=[self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle==nil) {
        return nil;
    }
    
    // Create label with section title
    UILabel *label=[[[UILabel alloc] init] autorelease];
    label.frame=CGRectMake(12, 0, 300, 22);
    label.backgroundColor=[UIColor clearColor];
    label.textColor=[UIColor MujiLightTextColor];
    label.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
    label.text=sectionTitle;
    
    // Create header view and add label as a subview
    UIView *sectionView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)] autorelease];
    [sectionView setBackgroundColor:[UIColor blackColor]];
    [sectionView addSubview:label];
    return sectionView;
}


發佈了19 篇原創文章 · 獲贊 2 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章