xcode 4.3.2 use storyboard创建TableView

xcode 4.3.2 use storyboard 使用TableView,显示一个颜色列表,表格包括一张图片,一个文本,一行副文本。

 

TableView 呈现列表格式的数据,每一行是一个UITableViewCell对象,每个UITableViewCell可以显示文本标签(为textLabel),字幕(detailedTextLabel)和图像(ImageView)。

每一个TableView,需要有与之相关的委托(delegate)和一个数据源(DataSource)。

委托实现UITableViewDelegate的协议,并提供额外的控制的外观和功能表视图,包括检测用户触摸时特定的行,自定义行高和缩进,并实施行删除和编辑功能。

数据源,实现UITableViewDataSource协议,基本上包含方法定义标题信息,要显示多少行数据,如何将数据划分成不同的部分。

 

1,新建Single View Application ,use storyboard。 

2,storyboard中ViewController中,拖入一个Table View ,设置TableView 的Outlets的dataSource和delegate 连线到View Controller

3,ViewController.h 

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
    NSArray *colorName;// 数据源
}
@property (nonatomic,retain) NSArray *colorName;

4,ViewController.m

@synthesize colorName;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 初始化加载视图时的一些数据
    colorName = [[NSArray alloc] initWithObjects:@"Red",@"Green",@"Yellow",@"Blue",@"White",@"Black", nil];
}
/**
 获取TableView显示的行数
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [colorName count];
}
/**
 定义Cell外观
 */
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    //设置图片,PolyStar.png是项目资源中的图片名
    UIImage *image = [UIImage imageNamed:@"PolyStar.png"];
    cell.imageView.image = image;
    //设置文字
    NSString *color = [colorName objectAtIndex:[indexPath row]];
    cell.textLabel.text = color;
    //设置详细介绍
    NSString *desc = [NSString stringWithFormat:@"关于颜色"];
    desc = [desc stringByAppendingFormat:color];
    cell.detailTextLabel.text = desc;
    
    return cell;
}

OK.

学习ing ...

 

转载于:https://www.cnblogs.com/tqspring/archive/2012/05/31/2528016.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值