#import "MainViewController.h"
#import "SubTableViewCell.h"
#import "ModelViewController.h"
#import "BoyTableViewCell.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.nameArray = [NSMutableArray arrayWithObjects:@"龙XX",@"王XX",@"贾XX",@"李XX",@"张XX",@"郑XX",@"马XX",@"王XX",@"马XX",@"尚XX",@"姜XX",@"付XX",@"张XX",@"臧XX",@"付XX",@"裴XX",@"谭XX",@"李XX",@"王XX",@"刘XX", nil];
self.sexArray = [NSMutableArray arrayWithObjects:@"男",@"男",@"男",@"男",@"女",@"男",@"男",@"男",@"男",@"男",@"男",@"女",@"女",@"男",@"男",@"女",@"男",@"男",@"男",@"男", nil];
self.telArray = [NSMutableArray arrayWithObjects:@"15542141542",@"15542141542",@"155445689542",@"15542141542",@"15542141542",@"15542141542",@"15542141542",@"15542141542",@"15572572",@"15563321542",@"155542442",@"15542141542",@"15542141542",@"154578979542",@"15542141542",@"11564684",@"4846234154",@"15542141542",@"15542141542",@"15542141542", nil];
self.ageArray = [NSMutableArray arrayWithObjects:@"25", @"28", @"58", @"14", @"11", @"22", @"23", @"27", @"24", @"32", @"29", @"22", @"22", @"27", @"23", @"20", @"21", @"29", @"23", @"32", nil];
self.stuArray = [[NSMutableArray alloc]init];
[self setUpArr];
}
return self;
}
-(void)dealloc
{
[_tableview release];
[_nameArray release];
[_sexArray release];
[_ageArray release];
[_telArray release];
[_stuArray release];
[super dealloc];
}
- (void)setUpArr
{
for (int i = 0 ; i < 20 ; i ++ ) {
ModelViewController * modell = [[ModelViewController alloc]init];
modell.name = [_nameArray objectAtIndex:i];
modell.sex = [_sexArray objectAtIndex:i];
modell.age = [_ageArray objectAtIndex:i];
modell.tel = [_telArray objectAtIndex:i];
[_stuArray addObject:modell];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.3 green:0.1 blue:0.3 alpha:0.5];
// self.navigationController.navigationBar.translucent = NO;
_tableview = [[UITableView alloc]initWithFrame:CGRectMake(10,0 ,300, 470) style:UITableViewStylePlain];
[self.view addSubview:_tableview];
[_tableview release];
_tableview.backgroundColor = [UIColor colorWithRed:0.7 green:0.7 blue:1 alpha:0.8];
_tableview.separatorColor = [UIColor blackColor];
_tableview.dataSource = self;
_tableview.delegate = self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.stuArray count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// NSString *cellID = @"啦啦啦";
// UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
// if (cell == nil) {
// cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
// }
// cell.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:0.8];
// cell.textLabel.text = [NSString stringWithFormat:@"%@",[_data objectAtIndex:indexPath.row]];
// NSString *cellID = @"啦啦啦";
// SubTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
// if (cell == nil) {
// cell = [[[SubTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
// }
// cell.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:0.8];
// cell.lable1.text = [[_stuArray objectAtIndex:indexPath.row] name];
// cell.lable2.text = [[_stuArray objectAtIndex:indexPath.row] sex];
// cell.lable3.text = [[_stuArray objectAtIndex:indexPath.row] tel];
// ModelViewController * stu = [self.stuArray objectAtIndex:indexPath.row];
// NSLog(@"%@",stu.name);
// //将数据model对象直接扔给cell
// cell.student = [_stuArray objectAtIndex:indexPath.row];
//当使用多个cell的时候
//取出相应地model
ModelViewController * stu = [self.stuArray objectAtIndex:indexPath.row];
if ([stu.sex isEqualToString:@"男"]) {
static NSString * boy = @"boy";
BoyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:boy];
if (cell == nil) {
cell = [[[BoyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:boy] autorelease];
}
cell.image.image = [UIImage imageNamed:[NSString stringWithFormat:@"a%d.jpg",indexPath.row+1 ]];
cell.student = stu;
return cell;
}else{
static NSString * girl = @"girl";
SubTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:girl];
if (cell == nil) {
cell = [[[SubTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:girl] autorelease];
}
cell.image.image = [UIImage imageNamed:[NSString stringWithFormat:@"a%d.jpg",indexPath.row+1 ]];
cell.student = stu;
return cell;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"啦啦啦 %d",indexPath.row);
}
#import "SubTableViewCell.h"
@implementation SubTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.lable1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 5, 130, 30)];
[self.contentView addSubview:_lable1];
[_lable1 release];
_lable1.backgroundColor = [UIColor cyanColor];
self.lable2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, 130, 30)];
[self.contentView addSubview:_lable2];
[_lable2 release];
_lable2.backgroundColor = [UIColor cyanColor];
self.lable3 = [[UILabel alloc]initWithFrame:CGRectMake(20, 75, 130, 30)];
[self.contentView addSubview:_lable3];
[_lable3 release];
_lable3.backgroundColor = [UIColor cyanColor];
self.image = [[UIImageView alloc]initWithFrame:CGRectMake(160, 10, 130, 90)];
[self.contentView addSubview:_image];
[_image release];
_image.backgroundColor = [UIColor cyanColor];
}
return self;
}
-(void)dealloc
{
[_lable1 release];
[_lable1 release];
[_lable3 release];
[_image release];
[super dealloc];
}
//重写model的setter方法
//作用 1 、 在setter方法中,model肯定有数据
// 2 、 setter方法中,能保证view上德值随着数据改变
- (void)setStudent:(ModelViewController *)student
{
if (_student != student) {
[_student release];
_student = [student retain];
}
self.lable1.text = student.name;
self.lable2.text = student.sex;
self.lable3.text = student.tel;
}