表视图

新建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

修改

修改title

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

   return [NSStringstringWithFormat:@"%d小节",section];

}

源码地址:http://download.csdn.net/detail/cloud95/5190554




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值