UITableView-(单元格的自定义方法)

//contentView

//行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //从重用队列中取出闲置单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //判断cell是否为nil
    if (cell == nil) {
        //创建单元格的时候,要确保重用标示符跟获取闲置单元格的时候一致
        cell = [[UITableViewCell alloc] initWithStyle:indexPath.row % 4 reuseIdentifier:identifier];
        //添加右边的图标
        UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(tableView.frame) - 60 - 20, 10, 60, 60)];
        [cell.contentView addSubview:iconView];
        iconView.tag = 101;
        
        //添加左边的标题Label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, CGRectGetWidth(cell.frame) - 100, 60)];
        [cell.contentView addSubview:titleLabel];
        titleLabel.tag = 102;
    }
    
    UIImageView *iconView = [cell.contentView viewWithTag:101];
    UILabel *titleLabel = [cell.contentView viewWithTag:102];
    
    //设置内容
    NSString *iconName = [NSString stringWithFormat:@"icon%ld.jpg",indexPath.row % 6];
    iconView.image = [UIImage imageNamed:iconName];
    
    titleLabel.text = self.datas[indexPath.row];
    
    return cell;
    
}

 

posted @ 2016-04-11 17:16  麦兜丶  阅读(200)  评论(0编辑  收藏  举报