tableView從XIB文件獲取cell

//  
    //  ViewController.h  
    //  TestCell  
    //  
    //  Created by Juncy_Fan on 13-3-11.  
    //  Copyright (c) 2013年 Juncy_Fan. All rights reserved.  
    //  
     
    #import <UIKit/UIKit.h>  
      
    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{  
        UITableView *table;  
        UINib *nib;  
    }  
      
    @end



//  
    //  ViewController.m  
    //  TestCell  
    //   
    //  
     
    #import "ViewController.h"  
    #import "MyCell.h"  
      
    @interface ViewController ()  
      
    @end  
      
    @implementation ViewController  
    static BOOL nibsRegistered;  
    - (void)viewDidLoad  
    {  
        [super viewDidLoad];  
        nibsRegistered = NO;  
        NSLog(@"初始化:%d",nibsRegistered);  
        table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];  
        [self.view addSubview:table];  
        table.delegate = self;  
        table.dataSource = self;  
        [table release];  
    }  
      
    - (void)didReceiveMemoryWarning  
    {  
        [super didReceiveMemoryWarning];  
        // Dispose of any resources that can be recreated.  
    }  
      
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{  
        return 300;  
    }  
      
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{  
        static NSString *identy = @"CustomCell";  
        if (!nib) {  
            nib = [UINib nibWithNibName:@"MyCell" bundle:nil];  
            [tableView registerNib:nib forCellReuseIdentifier:identy];  
            NSLog(@"從nib實現cell,%d",indexPath.row);  
        }  
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];  
        NSUInteger row = [indexPath row];  
        if (row%2 == 0) {  
            cell.myTitle.text = @"偶數行";  
            cell.mySubTitle.text = @"偶數行標題";  
            cell.imageicon.image = [UIImage imageNamed:@"201202.png"];  
        }else{  
            cell.myTitle.text = @"奇數行";  
            cell.mySubTitle.text = @"奇數行標題";  
            cell.imageicon.image = [UIImage imageNamed:@"201203.png"];  
        }  
        return cell;  
    }  
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
        return 100;  
    }  
      
    @end
相關文章
相關標籤/搜索