ios订制表视图

1、所创建的文件如下图

点击查看原图  

2、ViewController.xib文件如下图 

点击查看原图

3、SimpleTableCell.xib文件放入了3_UILable,1UIImageIdentifier属性填写SimpleTableCell

如下图

点击查看原图

4、ViewController.h中的关键代码

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{

    NSArray *tableData;

}

 

@end

5、ViewController.m中的关键代码 

首先#import “SimpleTableCellCell.h”

– (void)viewDidLoad

{

    [super viewDidLoad];

         // Do any additional setup after loading the view, typically from a nib.

    tableData = [NSArray arrayWithObjects:@”111″,@”222″,@”333″, nil];

}

 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tableData count];

}

 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *simpleTableIdentifier = @”SimpleTableCell”;

    SimpleTableCellCell *cell = (SimpleTableCellCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if(cell == nil){

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@”SimpleTableCell” owner:self options:nib];

        cell = [nib objectAtIndex:0];

    }

    //cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

    //cell.imageView.image = [UIImage imageNamed:@”Sunflower.gif”];

    cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];

    cell.showImage.image = [UIImage imageNamed:@”Sunflower.gif”];

    return cell;

}

6、SimpleTableCellCell.h关键代码

#import <UIKit/UIKit.h>

 

@interface SimpleTableCellCell : UITableViewCell{

   

}

@property(nonatomic,weak)IBOutlet UILabel *nameLabel;

@property(nonatomic,weak)IBOutlet UILabel *TimeLabel;

@property(nonatomic,weak)IBOutlet UIImageView *showImage;

 

@end

7、SimpleTableCellCell.m关键代码

#import “SimpleTableCellCell.h”

 

@implementation SimpleTableCellCell

 

@synthesize nameLabel;

@synthesize TimeLabel;

@synthesize showImage;

 

– (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // Initialization code

    }

    return self;

}

 

– (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];

 

    // Configure the view for the selected state

}

 

@end

 

最终运行效果

 点击查看原图

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注