UITableViewCell的自定义

//  TableViewCell.h
//  TableViewCellDemo
//
//  Created by renxuan on 15/8/5.
//  Copyright (c) 2015年 renxuan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell

@property (nonatomic, retain) UIImageView *lbImageView;
@property (nonatomic, retain) UILabel *lbName;
@property (nonatomic, retain) UILabel *lbInfo;
@end


//  TableViewCell.m
//  TableViewCellDemo
//
//  Created by renxuan on 15/8/5.
//  Copyright (c) 2015年 renxuan. All rights reserved.
//

#import "TableViewCell.h"

@implementation TableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        _lbName = [[UILabel alloc]initWithFrame:CGRectMake(70, 15, 200, 20)];
        self.lbName.font = [UIFont boldSystemFontOfSize:18];
        self.lbName.textColor = [UIColor grayColor];
        [self addSubview:_lbName];
        _lbInfo = [[UILabel alloc] initWithFrame:CGRectMake(250, 20, 200, 20)];
        [self addSubview:_lbInfo];
        self.lbInfo.font = [UIFont boldSystemFontOfSize:12];
        self.lbInfo.textColor = [UIColor orangeColor];
        _lbImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];
        [self addSubview:_lbImageView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end



//  ViewController.h
//  TableViewCellDemo
//
//  Created by renxuan on 15/8/5.
//  Copyright (c) 2015年 renxuan. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "TableViewCell.h"

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *m_arrNames;
    NSArray *m_arrinfos;
    NSArray *m_arrImages;
}

@end



//  ViewController.m
//  TableViewCellDemo
//
//  Created by renxuan on 15/8/5.
//  Copyright (c) 2015年 renxuan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width,self.view.frame.size.height-20 ) style:(UITableViewStylePlain)];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    
    
    m_arrImages = [NSArray arrayWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",nil];
    m_arrNames = [NSArray arrayWithObjects:@"走在冬夜的冷风中",@"你再说一遍",@"不做死就不会死",@"看有飞碟",@"哎愚蠢的人类",@"扯犊子尽瞎扯",@"走在冬夜的冷风中",@"你再说一遍",@"不做死就不会死",@"看有飞碟",@"哎愚蠢的人类",@"扯犊子尽瞎扯",@"走在冬夜的冷风中",@"你再说一遍",@"不做死就不会死",@"看有飞碟",@"哎愚蠢的人类",@"扯犊子尽瞎扯",nil];
    
    m_arrinfos = [NSArray arrayWithObjects:@"你说什么我听不到",@"你在逗我吗",@"揍得你不要不要的",@"看有人在作死",@"真的假的不敢相信",@"跪下唱征服吧",@"你说什么我听不到",@"你在逗我吗",@"揍得你不要不要的",@"看有人在作死",@"真的假的不敢相信",@"跪下唱征服吧",@"你说什么我听不到",@"你在逗我吗",@"揍得你不要不要的",@"看有人在作死",@"真的假的不敢相信",@"跪下唱征服吧",nil];
//    m_arrinfos.textColor = [UIColor redColor];
//    m_arrinfos.font = [UIFont boldSystemFontOfSize:20];
}
//显示状态栏
//[[[UIApplication sharedApplication] setStatusBarHidden:FALSE]];
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [m_arrinfos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    NSInteger row = indexPath.row;
    
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row % 2)
    {
        [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
    }else {
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    
//    cell.textLabel.backgroundColor = [UIColor clearColor];
//    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    
   //    NSUInteger row = [indexPath row];
//        //    把数组中的值赋给单元格显示出来
//    cell.textLabel.text = [self.dataList objectAtIndex:row];
    
      cell.lbImageView.image = [UIImage imageNamed:[m_arrImages objectAtIndex:row]];
      cell.lbInfo.text = [m_arrinfos objectAtIndex:row];
      cell.lbName.text = [m_arrNames objectAtIndex:row];
            //cell.backgroundColor = [UIColor grayColor];

    return cell;
}

@end

 此页面的实现为UITableView自定义页面,可以自由调节文字及图片的大小及位置。

TableViewCell.h用于声明lbImageView、lbName、lbInfo这三个成员变量,TableViewCell.m用来设置图片文字的大小及属性,ViewController.h用来声明数组,用于存储图片及文字的内容,ViewController.m实现数组内的内容及各行的参数。

猜你喜欢

转载自1395014506.iteye.com/blog/2234205