table范例

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view.
 5     //创建一个模拟从coreData创建的数据
 6     NSMutableArray *data = [NSMutableArray arrayWithObjects:
 7                             [NSDictionary dictionaryWithObjectsAndKeys:@"宫保鸡丁", @"name", @"鸡丁、辣椒", @"Element", @"小鸟依人", @"author", @"pic01", @"image", nil],
 8                             [NSDictionary dictionaryWithObjectsAndKeys:@"辣子鸡块", @"name", @"鸡丁、辣椒", @"Element", @"小鸟依人", @"author", @"pic01", @"image", nil],
 9                             [NSDictionary dictionaryWithObjectsAndKeys:@"小炒肉丝", @"name", @"鸡丁、辣椒", @"Element", @"小鸟依人", @"author", @"pic01", @"image", nil], nil];
10     //全局的数据对象
11     cookData = data;
12 }
13 
14 //table内容的列数
15 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
16     return [cookData count];
17 }
18 
19 //table cell的创建
20 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
21     static NSString *cookTableId = @"cookTable";
22     //从内存池中取出已存在cell
23     mxdCookCell *cell = (mxdCookCell *)[tableView dequeueReusableCellWithIdentifier:cookTableId];
24     //如果不存在则创建
25     if (cell == nil) {
26         //通过自定义的cookCell.xib模板创建cell
27         NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"cookCell" owner:self options:nil];
28         cell = [nib objectAtIndex:0];
29     }
30     //数据填充
31     NSDictionary *cookCell = [cookData objectAtIndex:indexPath.row];
32     cell.cookName.text = [cookCell objectForKey:@"name"];
33     cell.cookElement.text = [cookCell objectForKey:@"Element"];
34     cell.cookAuthor.text = [cookCell objectForKey:@"author"];
35     //查询image所在路径
36     NSString *path = [[NSBundle mainBundle] pathForResource:[cookCell objectForKey:@"image"] ofType:@"jpg"];
37     cell.cookImage.image = [UIImage imageWithContentsOfFile:path];
38     return cell;
39 }
40 
41 //设置cell高度
42 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
43     return 72;
44 }

 

posted @ 2013-03-23 18:04  张三的猫  阅读(188)  评论(0编辑  收藏  举报