#import "ViewController.h"
#import "BookCell.h"
#import "AdCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *_dataArr;
UITableView *_myTableView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
}
-(void)createData
{
//通过文件名,找到文件路径
NSString *path=[[NSBundle mainBundle]pathForResource:@"bookData" ofType:@"plist"];
//通过文件路径,读取文件内容(接收的容器需要和文件最外层的结构匹配)
NSArray *fileArr=[NSArray arrayWithContentsOfFile:path];
_dataArr =[[NSMutableArray alloc]init];
for (NSDictionary *dic in fileArr) {
BookModel *bm=[[BookModel alloc]init];
bm.icon=[dic objectForKey:@"icon"];
bm.price=[dic objectForKey:@"price"];
bm.title=[dic objectForKey:@"title"];
bm.detail=[dic objectForKey:@"detail"];
[_dataArr addObject:bm];
}
}
-(void)createUI
{
_myTableView =[[UITableView alloc]initWithFrame:self.view.bounds];
_myTableView.delegate=self;
_myTableView.dataSource=self;
[self.view addSubview:_myTableView];
}
#pragma mark - tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArr count]+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!indexPath.row) {
return 160;
}
return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!indexPath.row) {
static NSString *adId=@"asdf";
AdCell *ad=[tableView dequeueReusableCellWithIdentifier:adId];
if (!ad) {
ad=[[AdCell alloc]initWithStyle:0 reuseIdentifier:adId];
}
return ad;
}
static NSString *identifier=@"cellID";
BookCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell=[[BookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
BookModel *bm=[_dataArr objectAtIndex:indexPath.row-1];
[cell setContentsWithBookModel:bm];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface AdCell : UITableViewCell <UIScrollViewDelegate>
@end
#import "AdCell.h"
@implementation AdCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self createUI];
}
return self;
}
-(void)createUI
{
UIScrollView *sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)];
[self.contentView addSubview:sv];
[sv setContentOffset:CGPointMake(320, 0)];
for (int i=0; i<4; i++) {
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(320*(i+1), 0, 320, 160)];
iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d",i]];
iv.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGR:)];
[iv addGestureRecognizer:tapGR];
tapGR.view.tag = 10+i;
[sv addSubview:iv];
}
UIImageView *pre = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)];
pre.image = [UIImage imageNamed:@"image3"];
[sv addSubview:pre];
UIImageView *next = [[UIImageView alloc]initWithFrame:CGRectMake(320*5, 0, 320, 160)];
next.image = [UIImage imageNamed:@"image0"];
[sv addSubview:next];
sv.showsHorizontalScrollIndicator = NO;
sv.contentSize = CGSizeMake(320*6, 160);
sv.pagingEnabled = YES;
sv.delegate = self;
UIPageControl *pc = [[UIPageControl alloc]initWithFrame:CGRectMake(230, 140, 100, 20)];
pc.numberOfPages = 4;
pc.tag = 100;
[self.contentView addSubview:pc];
}
- (void)tapGR:(UITapGestureRecognizer *)tapGR
{
UIAlertView *av = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%d",tapGR.view.tag-9] delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[av show];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *pc = (UIPageControl *)[self.contentView viewWithTag:100];
NSInteger page=scrollView.contentOffset.x/320;
if (!page) {
[scrollView setContentOffset:CGPointMake(320*4, 0)];
pc.currentPage=4;
}
else if (page==5) {
[scrollView setContentOffset:CGPointMake(320, 0)];
pc.currentPage=0;
}else
{
pc.currentPage=page-1;
}
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
#import <Foundation/Foundation.h>
@interface BookModel : NSObject
@property (nonatomic,copy)NSString *detail;
@property (nonatomic,copy)NSString *icon;
@property (nonatomic,copy)NSString *price;
@property (nonatomic,copy)NSString *title;
@end
#import "BookModel.h"
@implementation BookModel
@end
#import <UIKit/UIKit.h>
#import "BookModel.h"
@interface BookCell : UITableViewCell
{
UIImageView *_iconView;//头像
UILabel *_titleLabel;//标题
UILabel *_priceLabel;//价格
UILabel *_detailLabel;//详情
}
//
-(void)setContentsWithBookModel:(BookModel *)bm;
@end
#import "BookCell.h"
@implementation BookCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self createUI];
}
return self;
}
-(void)createUI
{
_iconView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
[self.contentView addSubview:_iconView];
_titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 5, 200, 25)];
_titleLabel.font=[UIFont systemFontOfSize:16];
[self.contentView addSubview:_titleLabel];
_priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 30, 200, 25)];
_priceLabel.font=[UIFont systemFontOfSize:14];
[self.contentView addSubview:_priceLabel];
_detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(90, 55, 200, 25)];
_detailLabel.font=[UIFont systemFontOfSize:14];
[self.contentView addSubview:_detailLabel];
}
-(void)setContentsWithBookModel:(BookModel *)bm
{
_iconView.image=[UIImage imageNamed:bm.icon];
_titleLabel.text=bm.title;
_priceLabel.text=bm.price;
_detailLabel.text=bm.detail;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
————————————————————————————分割————————————————————————————
#import "ViewController.h"
#import "BookCell.h"
#import "BookModel.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray *_dataArr;
UITableView *_myTableView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
}
- (void)createData
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"bookData" ofType:@"plist"];
NSArray *fileArr = [NSArray arrayWithContentsOfFile:path];
_dataArr = [[NSMutableArray alloc] init];
for (NSDictionary *dic in fileArr) {
BookModel *bm = [[BookModel alloc] init];
bm.icon = [dic objectForKey:@"icon"];
bm.title = [dic objectForKey:@"title"];
bm.price = [dic objectForKey:@"price"];
bm.detail = [dic objectForKey:@"detail"];
[_dataArr addObject:bm];
}
}
- (void)createUI
{
_myTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_myTableView.dataSource = self;
_myTableView.delegate = self;
[self.view addSubview:_myTableView];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qqq"];
if (!cell) {
//通过nib文件加载cell
cell = [[[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil] lastObject];
}
BookModel *bm = [_dataArr objectAtIndex:indexPath.row];
cell.iconView.image = [UIImage imageNamed:bm.icon];
cell.titleLabel.text = bm.title;
cell.priceLabel.text = bm.price;
cell.detailLabel.text = bm.detail;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <Foundation/Foundation.h>
@interface BookModel : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *price;
@property (nonatomic, copy) NSString *detail;
@end
#import "BookModel.h"
@implementation BookModel
@end
#import <UIKit/UIKit.h>
@interface BookCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@end
#import "BookCell.h"
@implementation BookCell
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
————————————————————————————分割————————————————————————————
改动
#import "ViewController.h"
#import "BookCell.h"
#import "BookModel.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray *_dataArr;
UITableView *_myTableView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createData];
[self createUI];
}
- (void)createData
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"bookData" ofType:@"plist"];
NSArray *fileArr = [NSArray arrayWithContentsOfFile:path];
_dataArr = [[NSMutableArray alloc] init];
for (NSDictionary *dic in fileArr) {
BookModel *bm = [[BookModel alloc] init];
bm.icon = [dic objectForKey:@"icon"];
bm.title = [dic objectForKey:@"title"];
bm.price = [dic objectForKey:@"price"];
bm.detail = [dic objectForKey:@"detail"];
[_dataArr addObject:bm];
}
}
- (void)createUI
{
_myTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
_myTableView.dataSource = self;
_myTableView.delegate = self;
[self.view addSubview:_myTableView];
//通过文件名找到nib
UINib *cellNib = [UINib nibWithNibName:@"BookCell" bundle:nil];
//tv注册cell(将tv和cell关联起来)
[_myTableView registerNib:cellNib forCellReuseIdentifier:@"qqq"];
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"qqq" forIndexPath:indexPath];
BookModel *bm = [_dataArr objectAtIndex:indexPath.row];
cell.iconView.image = [UIImage imageNamed:bm.icon];
cell.titleLabel.text = bm.title;
cell.priceLabel.text = bm.price;
cell.detailLabel.text = bm.detail;
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end