定製單元格

1.UItTableViewController
標示圖控制器
兩種單元格類型
UITableViewCellStyleDefault   不支持子標題
UITableViewCellStyleSubtitle   支持子標題
都支持圖片與主題


另外兩種單元格類型
UITableViewCellStyleValue1  
UITableViewCellStyleValue2
都不支持 圖片


--------------------------定製單元格------------------------
1.通過UITableViewCell的contentView屬性添加子視圖
static NSString *identifier = @"firstId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]autorelease];
        /*
            不會改變的內容放在括號的裏面去寫
         */
        //創建子視圖
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(200, 0, 100, 44)];
        label.tag = 123;
        label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:label];
        [label release];
        
    }
    
    /*
         會改變的內容放在括號的外面面去寫
     */
    NSDictionary *dic = [_newsList objectAtIndex:indexPath.row];


    
    //獲取時間的文本
    UILabel *timeLabel = (UILabel *)[cell.contentView viewWithTag:123];
    timeLabel.text = [NSString stringWithFormat:@"%@小時之前",dic[@"time"]];
    
    cell.textLabel.text = dic[@"title"];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@條評論",[dic objectForKey:@"commentCount"]];
    




2.使用xib自定義視圖,佈局十分方便,開發較爲迅速
    if (cell == nil) {
        //同xib文件創建的
        /*
            用xib文件創建的時候
            一定要把UITableViewCell 的identifier屬性手動的設置上
         */
        cell = [[[NSBundle mainBundle]loadNibNamed:@"SecondCell" owner:self options:nil]lastObject];
        
    }
獲取工程主路徑  加載xib  選擇視圖


3.子類化UITableViewCell,更加面向對象


- (void)_initViews
{
    //創建標題文本
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:14];
    _titleLabel.textColor = [UIColor orangeColor];
    [self.contentView addSubview:_titleLabel];
//其他視圖
}
- (void)layoutSubviews
{
    //必須寫的**********
    [super layoutSubviews];
    
   //進行佈局
    _titleLabel.frame = CGRectMake(10, 5, 200, 20);
    _titleLabel.text = [self.dic objectForKey:@"title"];
//其他
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章