UITableView根本使用方法
UITableView基本使用方法
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize dataList = _dataList;
@synthesize tableView = _tableView;
-(void)viewDidLoad
{
[super viewDidLoad];
//初始化表格
UITableView *view = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView = view;
// 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
self.tableView.delegate = self;
self.tableView.dataSource = self;
//把tabView添加到视图之上
[self.view addSubview:self.tableView];
//存放显示在单元格上的数据
NSArray *list = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",nil];
self.dataList = list;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 声明静态字符串型对象,用来标记重用单元格
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
// 用TableSampleIdentifier表示需要重用的单元
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
// 如果如果没有多余单元,则需要创建新的单元
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
}
// 获取当前行信息值
NSUInteger row = [indexPath row];
// 把数组中的值赋给单元格显示出来
cell.textLabel.text = [self.dataList objectAtIndex:row];
//设置单元格背景颜色
//cell.textLabel.backgroundColor = [UIColor greenColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [UIColor orangeColor];
cell.backgroundView=backgroundView;
//添加图片
UIImage *image = [UIImage imageNamed:@"123.jpg"];
cell.imageView.image = image;
//被选中后高亮显示的图片1
UIImage *highLightImage = [UIImage imageNamed:@"1.jpg"];
cell.imageView.highlightedImage = highLightImage;
return cell;
}
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize dataList = _dataList;
@synthesize tableView = _tableView;
-(void)viewDidLoad
{
[super viewDidLoad];
//初始化表格
UITableView *view = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView = view;
// 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
self.tableView.delegate = self;
self.tableView.dataSource = self;
//把tabView添加到视图之上
[self.view addSubview:self.tableView];
//存放显示在单元格上的数据
NSArray *list = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",nil];
self.dataList = list;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.dataList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 声明静态字符串型对象,用来标记重用单元格
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
// 用TableSampleIdentifier表示需要重用的单元
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
// 如果如果没有多余单元,则需要创建新的单元
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
}
// 获取当前行信息值
NSUInteger row = [indexPath row];
// 把数组中的值赋给单元格显示出来
cell.textLabel.text = [self.dataList objectAtIndex:row];
//设置单元格背景颜色
//cell.textLabel.backgroundColor = [UIColor greenColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [UIColor orangeColor];
cell.backgroundView=backgroundView;
//添加图片
UIImage *image = [UIImage imageNamed:@"123.jpg"];
cell.imageView.image = image;
//被选中后高亮显示的图片1
UIImage *highLightImage = [UIImage imageNamed:@"1.jpg"];
cell.imageView.highlightedImage = highLightImage;
return cell;
}