简单的TableView通讯录

先定义了两个viewController

一个用来写通讯录的界面 一个显示详细信息

定义了Student类

Student.h

定义属性 初始化方法 便立构造器

@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, retain) NSString *image;

- (instancetype)initWithName:(NSString *)name number:(NSInteger)number image:(NSString *)image;

+ (instancetype)studentWithName:(NSString *)name number:(NSInteger)number image:(NSString *)image;

Student.m

- (void)dealloc
{
    [_name release];
    [_image release];
    [super dealloc];
}

- (instancetype)init
{
    self = [self initWithName:nil number:0 image:nil];
    return self;
}

- (instancetype)initWithName:(NSString *)name number:(NSInteger)number image:(NSString *)image
{
    self = [super init];
    if (self) {
        _name = name;
        _number = number;
        _image = image;
    }
    return self;
}

+ (instancetype)studentWithName:(NSString *)name number:(NSInteger)number image:(NSString *)image
{
    Student *student = [[Student alloc] initWithName:name number:number image:image];
    return [student autorelease];
}

MainViewController.h 

签订协议 定义一个数组

@interface MainViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *array;

@end


MainViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        Student *student1 = [Student studentWithName:@"艾希" number:1 image:@"1.jpg"];
        Student *student2 = [Student studentWithName:@"莫甘娜" number:2 image:@"2.jpg"];
        Student *student3 = [Student studentWithName:@"娜美" number:3 image:@"3.jpg"];
        Student *student4 = [Student studentWithName:@"阿狸" number:4 image:@"4.jpg"];
        Student *student5 = [Student studentWithName:@"娑娜" number:5 image:@"5.jpg"];
        Student *student6 = [Student studentWithName:@"奈德丽" number:6 image:@"6.jpg"];

        self.array = [NSMutableArray arrayWithObjects:student1, student2, student3, student4, student5, student6, nil];
        
    }
    return self;
}

- (void)dealloc
{
    [_array release];
    [super dealloc];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    
    [self.view addSubview:tableView];
    [tableView release];
    
    [tableView setRowHeight:50];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cellReuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str] autorelease];
    }
    
    Student *value = [self.array objectAtIndex:indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:value.image];
    cell.textLabel.text = value.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", value.number];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    Student *value = [self.array objectAtIndex:indexPath.row];
    firstVC.string = [NSString stringWithFormat:@"%@    %d", value.name, value.number];
    firstVC.string1 = value.image;
    [self.navigationController pushViewController:firstVC animated:YES];
    [firstVC release];
    
}

FirstViewController.h

我要用属性传值 所以定义了两个字符串 接收信息

@property (nonatomic, retain) NSString *string;
@property (nonatomic, retain) NSString *string1;

FirstViewController.m

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 110, 110)];
    [self.view addSubview:imageView];
    [imageView release];
    UIImage *image = [UIImage imageNamed:self.string1];
    imageView.image = image;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 250, 110, 50)];
    label.text = self.string;
    [self.view addSubview:label];


今天刚学 tableView 所以还不是很会 以后在慢慢完善这个好了







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值