标签:
@interface MyTableViewCell : UITableViewCell
@property (nonatomic, retain) UIImageView *headerImageView;
@property (nonatomic, retain) UILabel *titleLabel;
//@property (nonatomic, copy) NSString *image_nameLabel;
@property (nonatomic, retain) UILabel *priceLabel;
@property (nonatomic, retain) Watch *watch;
@end
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_headerImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_headerImageView];
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLabel.numberOfLines = 0;
[self.contentView addSubview:_titleLabel];
_priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_priceLabel];
}
return self;
}
- (void)setWatch:(Watch *)watch {
if (_watch != watch) {
[_watch release];
_watch = [watch retain];
_headerImageView.image = [UIImage imageNamed:_watch.image_name];
_titleLabel.text = _watch.title;
_titleLabel.numberOfLines = 0;
_priceLabel.text = _watch.price;
}
}
- (void)layoutSubviews {
[super layoutSubviews];
_headerImageView.frame = CGRectMake(5, 5, 100, 80);
_titleLabel.frame = CGRectMake(100, 12, 100, 300);
_priceLabel.frame = CGRectMake(250, 35, 100, 30);
}
@implementation MyTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_headerImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_headerImageView];
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLabel.numberOfLines = 0;
[self.contentView addSubview:_titleLabel];
_priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.contentView addSubview:_priceLabel];
}
return self;
}
- (void)setWatch:(Watch *)watch {
if (_watch != watch) {
[_watch release];
_watch = [watch retain];
_headerImageView.image = [UIImage imageNamed:_watch.image_name];
_titleLabel.text = _watch.title;
_titleLabel.numberOfLines = 0;
_priceLabel.text = _watch.price;
}
}
- (void)layoutSubviews {
[super layoutSubviews];
_headerImageView.frame = CGRectMake(5, 5, 100, 80);
_titleLabel.frame = CGRectMake(100, 12, 100, 300);
_priceLabel.frame = CGRectMake(250, 35, 100, 30);
}
//过程重点
@implementation RootViewController
- (void)dealloc {
[_scrollView release];
[_pageControl release];
[_tableView release];
[super dealloc];
}
- (void)loadData {
self.dataList = [NSMutableDictionary dictionaryWithCapacity:0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoodsList" ofType:@"plist"];
NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
for (NSDictionary *dic in arr) {
NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:0];
for (NSDictionary *itemDic in dic[@"kind"]) {
Watch *watch = [[Watch alloc] init];
[watch setValuesForKeysWithDictionary:itemDic];
[mArr addObject:watch];
[watch release];
}
[_dataList setValue:mArr forKey:dic[@"name"]];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadData];
// self.automaticallyAdjustsScrollViewInsets = NO;
self.navigationItem.title = @"智能手表";
[self loadScroll];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.tableHeaderView = _scrollView;
NSLog(@"%@",NSStringFromCGRect(self.view.frame));
[_tableView addSubview:_pageControl];
[self.view addSubview:_tableView];
}
/**
* 滚动视图和pageControl
*/
- (void)loadScroll {
CGFloat scrollH = KScrollHeight;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KWidth, scrollH)];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.contentSize = CGSizeMake(5 * KWidth, scrollH);
_scrollView.delegate = self;
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180, KWidth, 20)];
_pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
_pageControl.pageIndicatorTintColor = [UIColor whiteColor];
_pageControl.numberOfPages = 5;
for (int i = 0; i < 5; i ++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * KWidth, 0, KWidth, scrollH)];
imageView.image = [UIImage imageNamed:@"header_bg"];
[_scrollView addSubview:imageView];
[imageView release];
}
// [self.view addSubview:_scrollView];
// _tableView.tableHeaderView = _scrollView;
}
/**
* 当视图停止滚动
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//获取偏移量
CGPoint offset = scrollView.contentOffset;
_pageControl.currentPage = offset.x / KWidth;
}
#pragma mark ----数据源协议-----
/**
* 分区数
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_dataList.allKeys count];
}
/**
* 每个分区的行数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_dataList.allValues[section] count];
}
/**
* cell显示的内容
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = @"reuse";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier] autorelease];
}
Watch *watch = _dataList.allValues[indexPath.section][indexPath.row];
cell.watch = watch;
NSLog(@"%@",cell.watch.price);
// cell.imageView.image = [UIImage imageNamed:watch.image_name];
// cell.textLabel.text = watch.price;
// cell.titleLabel.text = watch.title;
// cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
/**
* 分区标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return _dataList.allKeys[section];
}
/**
* 行高
*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 300;
}
/**
* 点击单元格触发事件
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.watch = _dataList.allValues[indexPath.section][indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
}
@interface DetailViewController : UIViewController
@property (nonatomic, retain) Watch *watch;
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.title = self.watch.title;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 150, 300, 350)];
imageView.image = [UIImage imageNamed:self.watch.image_name];
[self.view addSubview:imageView];
[imageView release];
#import <Foundation/Foundation.h>
@interface Watch : NSObject
@property (nonatomic, copy) NSString *ID;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *image_name;
@property (nonatomic, copy) NSString *price;
@implementation Watch
- (void)dealloc {
[_ID release];
[_title release];
[_desc release];
[_image_name release];
[_price release];
[super dealloc];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
if ([key isEqualToString:@"id"]) {
self.ID = value;
} else if ([key isEqualToString:@"description"]) {
self.desc = value;
}
}
通过例题可以更好的巩固知识点。
标签:
原文地址:http://www.cnblogs.com/wwww543623/p/5171048.html