XIB自定义单元格

//
//  ViewController.m
//  UITableView-XIB
//
//  Created by jay on 15/12/3.
//  Copyright (c) 2015年 jay. All rights reserved.
//

#import "ViewController.h"
#import "Book.h"
#import "BookTableViewCell.h"
@interface ViewController ()
@property(strong,nonatomic) NSMutableArray *dataList;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.dataList=[NSMutableArray arrayWithCapacity:30];
    
    Book *book;
    
    for (NSInteger i=0; i<30; i++) {
        book=[Book alloc];
        book.bookName=[NSString stringWithFormat:@"ios-%ld",(long)i];
        book.price=58.98;
        [self.dataList addObject:book];
    }
    // Do any additional setup after loading the view, typically from a nib.
    UINib *nib=[UINib nibWithNibName:@"BookCell" bundle:[NSBundle mainBundle]];
    
    UITableView *tableView=(UITableView *)self.view;
    
    [tableView registerNib:nib forCellReuseIdentifier:@"bookCell"];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier=@"bookCell";
    
    BookTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Identifier];
    
    /*if (cell==nil) {
        NSLog(@"xx");
        NSBundle *bundle=[NSBundle mainBundle];
        NSArray *views = [bundle loadNibNamed:@"BookCell" owner:nil options:nil];
        cell=[views lastObject];
    }*/
    
    Book *book=self.dataList[indexPath.row];
    
    [cell.bookNameLabel setText:book.bookName];
    
    [cell.priceLabel setText:[NSString stringWithFormat:@"%.2f",book.price]];
    
    return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

posted on 2015-12-03 23:22  jayhtt  阅读(91)  评论(0)    收藏  举报