天天看點

cell的定制界面

1.繼承自UITableViewCell類,建立自定義cell類 1.1添加需要顯示的屬性字段

@interface MyTableViewCell ()

{

    //儲存應用程式圖示

    UIImageView *_iconView;

    //儲存公司的名稱

    UILabel *_publishView;

    //應用程式名稱

    UILabel *_nameView;

    //儲存價格

    UILabel *_priceView;

    //評分的人數

    UILabel *_numRatingsView;

    //評分的分數

    UIView *_ratingView; 

}

@end

1.2在類的初始化方法中定義初始化屬性字段并添加到自身的contentView視圖上

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        //1.建立應用圖示

        _iconView = [[UIImageView alloc]init];

        _iconView.frame = CGRectMake(5, 5, 60, 60);

        [self.contentView addSubview:_iconView];

        //2.建立公司名稱

        _publishView = [[UILabel alloc]init];

        _publishView.frame = CGRectMake(90, 5, 250, 20);

        [self.contentView addSubview:_publishView];

        //3.應用名稱

        _nameView = [[UILabel alloc]init];

        _nameView.frame = CGRectMake(90, 30, 200, 25);

        [self.contentView addSubview:_nameView];

        //4.價格

        _priceView = [[UILabel alloc]initWithFrame:CGRectMake(300, 30, 60, 25)];

        [self.contentView addSubview:_priceView];

        //5.評分分數

        _ratingView = [[UIView alloc]initWithFrame:CGRectMake(90, 65, 100, 23)];

        _ratingView.backgroundColor = [UIColor lightGrayColor];

        [self.contentView addSubview:_ratingView];

        //6.評分人數

        _numRatingsView = [[UILabel alloc]initWithFrame:CGRectMake(200, 65, 100, 20)];

        [self.contentView addSubview:_numRatingsView];

    }

    return self;

}

2.在控制器的表視圖的資料源協定方法的定義cell方法中執行個體化自定義類,并指派相應的資料

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *cellID = @"cellID";

    //建立自定義cell

    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if (cell == nil) {

        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

    }

    //設定要顯示的内容

    //1.對自定義cell的相關屬性指派資料

    NSDictionary *dict = [self.arrayData objectAtIndex:indexPath.row];

    cell.iconView.image = [UIImage imageNamed:[dict objectForKey:@"Icon"]];

    cell.publishView.text = [dict objectForKey:@"Publisher"];

    cell.priceView.text = [dict objectForKey:@"Price"];

    cell.nameView.text = [dict objectForKey:@"Name"];

    cell.numRatingsView.text = [[dict objectForKey:@"NumRatings"] stringValue];   

    return cell;

}