制作tableView视图

1.制作tableView视图

 

1。在.h文件下:

@interfaceMainViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

 

@end

 

注释:

UITableViewDelegate,UITableViewDataSource统称为协议。

UITableViewDataSource:连接数据和表视图

协议定了2个要求实现的方法

    (tableView:cellForRowAtIndexPath

       tableView:numberOfRowInSection

      告诉表视图显示多少行数据和每一行中的数据。

UITableViewDelegate:负责处理UITableView的表现。(协议中可以管理表行的高度,配置节点头部和底部,对表单元重新等)

 

2.在.m文件下:

//

//  MainViewController.m

//  Ex

//

//  Created by xpp on 13-11-14.

//  Copyright (c) 2013 tci . All rights reserved.

//

 

#import "MainViewController.h"

 

@interface MainViewController ()

 

@end

 

@implementationMainViewController

 

NSArray* tabelData;

 

 

//实例化tabelData,即要显示的数据。

- (void)viewDidLoad //在控制器的视图装载到内存中完成之后,调用该方法。

{

    [super viewDidLoad];

         // Do anyadditional setup after loading the view, typically from a nib.

    tabelData= [NSArray arrayWithObjects:@"egg",@"hamburger",@"breakfast",@"ham",@"noodle",@"coffee",@"egg",@"hamburger",@"breakfast",@"ham",@"noodle",@"coffee",@"egg",@"hamburger",@"breakfast",@"ham",@"noodle",@"coffee",@"egg",@"hamburger",@"breakfast",@"ham",@"noodle",@"coffee",nil];

}

 

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{//该方法是为表视图提供分段的个数(所有的列表项个数)

    NSLog(@"%i",tabelData.count);

     return[tabelData count];

 

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose ofany resources that can be recreated.

}

 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//提供表视图单元格所需要的数据。   

    static NSString* simpleTableIdentifier = @"SimpleTableItem";

    //该语句为表视图单元格提供了一个标识,当上面的单元格滚出屏幕,下面的单元格滚入屏幕时候,可以通过判断这个标识是否有可以重用的单元格,如果有则重用,如果没有则创建一个新的。

    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell== nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];

    }

    cell.textLabel.text = [tabelDataobjectAtIndex:indexPath.row];   

   

    returncell;

}

 

@end

 

3.连接数据源和委托

表视图单元格的单元元素:

   图片 cell.imageView.image

   文本标签 cell.textLabel.text 

详细文本标签 cell.detailTextLabel.text

http://www.cnblogs.com/syxchina/archive/2012/09/08/2677007.html

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值