您的位置:首页 > 产品设计 > UI/UE

UITableViewCell的简单应用-书架

2014-06-18 20:19 197 查看
@interface
MainViewController ()

@property (nonatomic,
retain)NSMutableArray *array;

@property (nonatomic,
retain)UITableView *tableView;

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if (self) {

// Custom initialization

// self.array = [NSMutableArray arrayWithObjects: @"玄幻", @"言情", @"耽美",@"都市"
,@"网游", nil];

NSMutableArray *arr1 = [NSMutableArray
arrayWithObjects:[NSMutableDictionary
dictionaryWithObjectsAndKeys:@"斗破苍穹",
@"title", @"k.png",
@"image", @"天蚕土豆",
@"author", nil], [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"完美世界",
@"title", @"k.png",
@"image", @"辰东",
@"author", nil], [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"仙逆",
@"title", @"4.png",
@"image", @"耳根",
@"author", nil],nil];

NSMutableArray *arr2 = [NSMutableArray
arrayWithObjects:[NSMutableDictionary
dictionaryWithObjectsAndKeys:@"当你孤单你会想起谁",
@"title", @"k.png",
@"image", @"饶雪漫",
@"author", @"cicicici1",
@"intro", nil], [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"放弃你,下辈子吧!",
@"title", @"k.png",
@"image", @"桩桩",
@"author", @"cicicici2",
@"intro", nil], [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"独家记忆",
@"title", @"k.png",
@"image", @"木浮生",
@"author", @"cicicici3",
@"intro", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"终有一爱",
@"title", @"k.png",
@"image", @"金陵雪",
@"author", @"cicicici4",
@"intro", nil],nil];

self.array = [NSMutableArray
arrayWithObjects:[NSMutableDictionary
dictionaryWithObjectsAndKeys:@"玄幻",@"type",
arr1, @"book",
nil], [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"言情",@"type",
arr2, @"book",
nil],
nil];

self.tableView = [[UITableView
alloc] init];
[_tableView
release];

self.title =
@"书架";
}

return
self;
}

- (void)dealloc
{

[_tableView
release];

[_array
release];
[super
dealloc];

}
- (void)viewDidLoad
{

[super
viewDidLoad];

// Do any additional setup after loading the view.

self.tableView = [[UITableView
alloc] initWithFrame:CGRectMake(0,
0, 320,
480)
style:(UITableViewStyleGrouped)];

self.tableView.delegate =
self;

self.tableView.dataSource =
self;
[self.view
addSubview:self.tableView];

[_tableView
release];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{

NameViewController *nameVC = [[NameViewController
alloc]
init];
nameVC.self.title = [[self.array
objectAtIndex:indexPath.row]
objectForKey:@"type"];
nameVC.self.array1 = [[self.array
objectAtIndex:indexPath.row]
objectForKey:@"book"];

[self.navigationController
pushViewController:nameVC animated:YES];

[tableView deselectRowAtIndexPath:indexPath
animated:YES];
}

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

return [self.array
count];

}

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

static NSString *str =
@"kzss";

MyCellTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:str];

if (nil == cell) {

cell = [[[MyCellTableViewCell
alloc] initWithStyle:(UITableViewCellStyleSubtitle)
reuseIdentifier:str] autorelease];
}

NSString *name = [[self.array
objectAtIndex:indexPath.row]
objectForKey:@"type"];
cell.myTitleLabel.text = name;

return cell;

}
- (void)didReceiveMemoryWarning
{

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}

@end

@interface NameViewController :
UIViewController<UITableViewDataSource,
UITableViewDelegate>

@property (nonatomic,
retain)NSMutableArray *array1;

@end

#import "NameViewController.h"

#import "BookViewController.h"

#import "MyCellTableViewCell.h"

@interface
NameViewController ()

@end

@implementation NameViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if (self) {

// Custom initialization

self.array1 = [NSMutableArray
array];
}

return
self;
}

- (void)dealloc
{
[_array1
release];
[super
dealloc];

}
- (void)viewDidLoad
{

[super
viewDidLoad];

// Do any additional setup after loading the view.

UITableView *table = [[UITableView
alloc] initWithFrame:CGRectMake(0,
0, 320,
480)
style:(UITableViewStyleGrouped)];
table.delegate =
self;
table.dataSource =
self;
[self.view
addSubview:table];
[table
release];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{

BookViewController *bookVC = [[BookViewController
alloc]
init];
bookVC.title = [[self.array1
objectAtIndex:indexPath.row]
objectForKey:@"title"];
bookVC.name3 = [[self.array1
objectAtIndex:indexPath.row]
objectForKey:@"author"];
bookVC.imagename = [[self.array1
objectAtIndex:indexPath.row]
objectForKey:@"image"];

[self.navigationController
pushViewController:bookVC animated:YES];
[bookVC
release];

}

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

return [self.array1
count];
}

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

static NSString *str1 =
@"aa";

MyCellTableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:str1];

if (nil == cell) {

cell = [[[MyCellTableViewCell
alloc] initWithStyle:(UITableViewCellStyleSubtitle)
reuseIdentifier:str1] autorelease];
}

NSString *name = [[self.array1
objectAtIndex:indexPath.row]
objectForKey:@"title"];
cell.myTitleLabel.text = name;

return cell;
}
- (void)didReceiveMemoryWarning
{

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}

@end

@interface BookViewController :
UIViewController

@property (nonatomic,retain)UILabel *name3;

@property (nonatomic,retain)UILabel *from;

@property (nonatomic,retain)NSString *imagename;

@end

@interface
BookViewController ()

@end

@implementation BookViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{

self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];

if (self) {

// Custom initialization

}

return
self;
}

- (void)viewDidLoad
{

[super
viewDidLoad];

// Do any additional setup after loading the view.

NSString *name = self.imagename;

UIImageView *imagek = [[UIImageView
alloc] initWithFrame:(CGRectMake(10,
70, 200,
200))];
imagek.backgroundColor =[UIColor
cyanColor];

imagek.image =[UIImage
imageNamed:name];
[self.view
addSubview:imagek];
[imagek
release];

UILabel *label = [[UILabel
alloc] initWithFrame:(CGRectMake(220,
70, 80,
60))];

label.textAlignment =
NSTextAlignmentCenter;
label.text = [NSString
stringWithFormat:@"%@",
self.name3];
[self.view
addSubview:label];
[label
release];

UILabel *label1 = [[UILabel
alloc] initWithFrame:(CGRectMake(220,
150, 80,
60))];
label1.backgroundColor = [UIColor
greenColor];
[self.view
addSubview:label1];
[label1
release];

UIImageView *image1 = [[UIImageView
alloc] initWithFrame:(CGRectMake(10,
275, 300,
200))];
image1.backgroundColor =[UIColor
blueColor];
image1.image = [UIImage
imageNamed:name];
[self.view
addSubview:image1];
[image1
release];

}

- (void)didReceiveMemoryWarning
{

[super
didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.
}

@end

@interface MyCellTableViewCell :
UITableViewCell

@property (nonatomic,
retain)UILabel *myTitleLabel;

@end

@implementation MyCellTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier
{

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

if (self) {

// Initialization code

self.myTitleLabel = [[UILabel
alloc] init];

self.myTitleLabel.textAlignment
= NSTextAlignmentCenter;
[self.contentView
addSubview:self.myTitleLabel];

[_myTitleLabel
release];
}

return
self;
}

- (void)dealloc
{

[_myTitleLabel
release];
[super
dealloc];
}

- (void)layoutSubviews
{

[super
layoutSubviews];

self.myTitleLabel.frame =
CGRectMake(0,
0, self.frame.size.width,
self.frame.size.height);

}- (void)awakeFromNib
{

// Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super
setSelected:selected
animated:animated];

// Configure the view for the selected state
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: