表格01--用户自定义tableViewCell(带有xib文件的)

1.新建一个single view controller

2.在项目下新建file->iOS->cocoa Touch ->Objective-C class 并使其继承UITableViewCell类MyCell01

在.h文件中声明两个输出口,即:

@property(nonatomic,retain)IBOutletUILabel *labName;

@property(nonatomic,retain)IBOutletUILabel *labGroup;

3.在该项目下新建file->ios->User Interface->Empty命名MyCell01      即MyCell01.xib

 在该文件拖一个tableViewCell 控件

并且把四个lable控件拖入其中:即


   打开MyCell01.xib文件使custom class 属性 中class:设定为MyCell01,然后点击file owner,进行连接labName和labGroup。

4.viewController.xib文件中拖进一个tableView,然后连接delegate 和dataSource

5.在viewController.h中实现两个协议:

<UITableViewDataSource,UITabBarDelegate>

 添加:

@property(nonatomic,strong)NSArray*computers;

6.在ViewController.m中写以下方法

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    NSDictionary *row1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"MacBook",@"Name",@"White",@"Color", nil];
    NSDictionary *row2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"MacBook Pro",@"Name",@"Sliver",@"Color", nil];
    NSDictionary *row3 = [[NSDictionary alloc]initWithObjectsAndKeys:@"iMac",@"Name",@"White",@"Color", nil];
    NSDictionary *row4 = [[NSDictionary alloc]initWithObjectsAndKeys:@"MacMini",@"Name",@"Red",@"Color", nil];
    self.computers =[[NSArray alloc]initWithObjects:row1,row2,row3,row4, nil];
}
//1返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [self.computers count];
}
//2绘制表格行
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //定义两个重用
    static NSString *CellTableIdentifier = @"CellTableIdentifier";
    static NSString *SelfCellIdentifier = @"SelfCellIdentifier";
    static BOOL nibsRegistered = NO;
    NSDictionary *rowData;
    NSUInteger row = [indexPath row];
    if (row%2 == 0) {
        if (!nibsRegistered) {
            UINib *nib = [UINib nibWithNibName:@"MyXIBCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:CellTableIdentifier];
            nibsRegistered = YES;
        }
        //测试
        NSLog(@"row0 = %d",row);
        rowData = [self.computers objectAtIndex:row];
        MyXIBCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];
        cell.lblName.text = [rowData objectForKey:@"Name"];
        cell.lblColor.text = [rowData objectForKey:@"Color"];
        return cell;
    }else{
        MyCell *cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SelfCellIdentifier];
               NSLog(@"row1 = %d",row);
        rowData = [self.computers objectAtIndex:row];
        NSLog(@"name = %@",[rowData objectForKey:@"Name"]);
        cell.lblName.text = [[self.computers objectAtIndex:row]objectForKey:@"Name"];
        cell.lblColor.text = [rowData objectForKey:@"Color"];
        return cell;

    
    }
    
      
}

//
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([indexPath  row]%2!=0) {
        return 80;
    }
    return 65;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

以上即可完成。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值