新建singleView Application,名为TableViewDemo.
创建一个Objective-c class,父类为UITableViewController,名为tableViewController.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
TableView中有几节(行)相当于一个group{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
每节(行)中有几列 一个group中有几行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
每一行,小节和列的位置
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
// Configure the cell...
if (!cell) {
cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"]autorelease];
}
cell.textLabel.text=[NSStringstringWithFormat:@"第%d行",indexPath.row];
return cell;
}
设置行高- (void)viewDidLoad
{
[superviewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.tableView.rowHeight=80;
}
设置颜色- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
// Configure the cell...
if (!cell) {
cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"]autorelease];
}
cell.textLabel.text=[NSStringstringWithFormat:@"第%d行",indexPath.row];
cell.textLabel.textColor=[UIColorblueColor];//设置颜色
return cell;
}
奇数行和偶数行不一样- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
// Configure the cell...
if (!cell) {
cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"]autorelease];
}
cell.textLabel.text=[NSStringstringWithFormat:@"第%d行",indexPath.row];
if(indexPath.row%2==1)
{
cell.textLabel.textColor=[UIColorgreenColor];//设置颜色
}
else
{
cell.textLabel.textColor=[UIColorblueColor];//设置颜色
}
return cell;
}
修改单元格样式定义一个macro来表示不同的单元格样式1-4
#define CellStyle 4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
// Configure the cell...
if (!cell) {
switch(CellStyle)
{
case1:cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"]autorelease];
break;
case2:cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:@"cell"]autorelease];
break;
case3:cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleValue1reuseIdentifier:@"cell"]autorelease];
break;
case4:cell=[[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleValue2reuseIdentifier:@"cell"]autorelease];
break;
}
}
cell.textLabel.text=[NSStringstringWithFormat:@"第%d行",indexPath.row];
if(CellStyle>1) {
cell.detailTextLabel.text=@"随便写点什么";
}
if(indexPath.row%2==1)
{
cell.textLabel.textColor=[UIColorgreenColor];//设置颜色
}
else
{
cell.textLabel.textColor=[UIColorblueColor];//设置颜色
}
return cell;
}
加图片cell.imageView.image=[UIImageimageNamed:@"logo"];
设置缩进
cell.indentationLevel=indexPath.row;
点击事件 ,选中了哪行- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
UIAlertView *alert=[[[UIAlertViewalloc] initWithTitle:@"选中了"message:[NSStringstringWithFormat:@"你选中了第%d行",indexPath.row] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
[alertshow];
}
创建tableView时init默认是UITableViewStylePlain
还有另外一种style UITableViewStyleGrouped
修改背景色
cell.backgroundColor=[UIColor yellowColor];
当style为 UITableViewStylePlain时,要在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
修改不然可以在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
修改
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSStringstringWithFormat:@"第%d小节",section];
}
源码地址:http://download.csdn.net/detail/cloud95/5190554