首页 > 其他 > 详细

小型的汽车浏览界面(目前功能比较简单)

时间:2015-03-24 11:09:09      阅读:107      评论:0      收藏:0      [点我收藏+]

Model --- 模型.h

#import <Foundation/Foundation.h>


@interface CarModel : NSObject


@property (nonatomic, copy) NSString *carName;

@property (nonatomic, copy) NSString *carPrice;

@property (nonatomic, copy) NSString *carDetail;

@property (nonatomic, copy) NSString *carImage;

@property (nonatomic, assign) NSInteger tag;


- (instancetype)initWithName:(NSString *)name price:(NSString *)price detail:(NSString *)detail image:(NSString *)image;

+ (instancetype)carWithName:(NSString *)name price:(NSString *)price detail:(NSString *)detail image:(NSString *)image;


@end 

Model --- 模型.m

#import "CarModel.h"


@implementation CarModel


- (void)dealloc

{

    [_carDetail release];

    [_carImage release];

    [_carName release];

    [_carPrice release];

    

    [super dealloc];

}


- (instancetype)initWithName:(NSString *)name price:(NSString *)price detail:(NSString *)detail image:(NSString *)image

{

    self = [super init];

    if (self) {

        self.carImage = image;

        self.carDetail = detail;

        self.carName = name;

        self.carPrice = price;

    }

    return self;

}

+ (instancetype)carWithName:(NSString *)name price:(NSString *)price detail:(NSString *)detail image:(NSString *)image

{

    CarModel *carModel = [[CarModel alloc] initWithName:name price:price detail:detail image:image];

    return [carModel autorelease];

}



@end


View - .h

#import <UIKit/UIKit.h>


@interface CustomTableViewCell : UITableViewCell


@property (nonatomic, retain) UIImageView *cusImageView;

@property (nonatomic, retain) UILabel *cusLabel;


@end


View - .m

#import "CustomTableViewCell.h"


@implementation CustomTableViewCell


- (void)dealloc

{

    [_cusLabel release];

    [_cusImageView release];

    

    [super dealloc];

}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

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

    if (self) {

        self.cusImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 100, 40)];

        [self.contentView addSubview:_cusImageView];

        [_cusImageView release];

        

        self.cusLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, 0, 100, 40)];

        [self.contentView addSubview:_cusLabel];

        [_cusLabel release];

    }

    return self;

}


- (void)awakeFromNib {

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state

}


@end


/******************************/

Controller - 文件

/******************************/

#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController 


@property (nonatomic, retain) NSMutableArray *tableArray;


@end


#import "MainViewController.h"

#import "CarModel.h"

#import "DetailViewController.h"

#import "SecondViewController.h"

#import "FirstViewController.h"

#import "CustomTableViewCell.h"


@interface MainViewController () <UITableViewDataSource, UITableViewDelegate, sendTableViewCell>

{

    UITableView *_table;

}

@end


@implementation MainViewController


- (void)dealloc

{

    [_tableArray release];

    

    [super dealloc];

}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

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

    if (self) {

        self.tableArray = [NSMutableArray array];

        

        CarModel *car1 = [CarModel carWithName:@"宝马-X1" price:@"100,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽台宝马汽车,我非常喜欢宝马汽车,你觉得呢?台宝马汽车,我非常喜欢宝马汽车,你觉得呢台宝马汽1111111111111111111" image:@"1.jpg"];

        CarModel *car2 = [CarModel carWithName:@"宝马-X2" price:@"200,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"2.jpg"];

        CarModel *car3 = [CarModel carWithName:@"宝马-X3" price:@"300,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"3.jpg"];

        CarModel *car4 = [CarModel carWithName:@"宝马-X4" price:@"400,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"4.jpg"];

        CarModel *car5 = [CarModel carWithName:@"宝马-X5" price:@"500,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"5.jpg"];

        CarModel *car6 = [CarModel carWithName:@"宝马-X6" price:@"600,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"6.jpg"];

        CarModel *car7 = [CarModel carWithName:@"宝马-X7" price:@"700,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"7.jpg"];

        CarModel *car8 = [CarModel carWithName:@"宝马-X8" price:@"800,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"8.jpg"];

        CarModel *car9 = [CarModel carWithName:@"宝马-X9" price:@"900,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"9.jpg"];

        

        NSMutableArray *first = [NSMutableArray array];

        [first addObject:car1];

        [first addObject:car2];

        [first addObject:car3];

        [first addObject:car4];

        [first addObject:car5];

        [first addObject:car6];

        [first addObject:car7];

        [first addObject:car8];

        [first addObject:car9];

        

        [_tableArray addObject:first];

        // 数组-》数组-model


        NSMutableArray *second = [NSMutableArray array];

        

        CarModel *car21 = [CarModel carWithName:@"宝马1" price:@"1100,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"10.jpg"];

        CarModel *car22 = [CarModel carWithName:@"宝马2" price:@"2100,000" detail:@"这是一台宝马汽车,我非常喜欢宝马汽车,你觉得呢?" image:@"11.jpg"];

        [second addObject:car21];

        [second addObject:car22];

        

        [_tableArray addObject:second];

        

    }

    return self;

}


#pragma mark- 

#pragma mark 创建界面


- (void)createTableView

{

    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];

    [_table setDataSource:self];

    [_table setDelegate:self];

    [self.view addSubview:_table];

    [_table release];

}


#pragma mark - 

#pragma mark tableView的协议方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [_tableArray count];

}


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

{

    return [[_tableArray objectAtIndex:section] count];

}


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

{

    static NSString *cellIdentify = @"Identify";

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];

    if (!cell) {

        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];

    }

    

    NSMutableArray *first = [_tableArray objectAtIndex:indexPath.section];

    CarModel *carModels = [first objectAtIndex:indexPath.row];

    [cell.cusImageView setImage:[UIImage imageNamed:carModels.carImage]];

    cell.cusLabel.text = carModels.carName;

    

    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 50.0f;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self createTableView];

    self.navigationItem.title = @"一个卖小汽车de地方";

    self.automaticallyAdjustsScrollViewInsets = NO;

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(turnNext)];

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)sectio

{

    return 50.0f;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    

    UILabel *carLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

    carLabel.textAlignment = NSTextAlignmentCenter;

    carLabel.userInteractionEnabled = YES;

    if (section == 0) {

        carLabel.text = @"宝马X系列";

        

    }else if(section == 1){

        carLabel.text = @"宝马其他系列";

    }

    UIButton *carButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [carButton setFrame:CGRectMake(self.view.frame.size.width - 80, 15, 60, 40)];

    [carLabel addSubview:carButton];

    carButton.tag = section + 100;

    [carButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [carButton setTitle:@"more" forState:UIControlStateNormal];

    [carButton addTarget:self action:@selector(moreButtonClick:) forControlEvents:UIControlEventTouchUpInside];

   

    return [carLabel autorelease];

    

}


#pragma mark - 

#pragma mark 跳转到添加页面

- (void)turnNext

{

    SecondViewController *second = [[SecondViewController alloc] init];

    second.delegate = self;

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

    [second release];

}


#pragma mark -

#pragma mark more功能键(跳转到FirstViewController)

- (void)moreButtonClick:(id)sender

{

    UIButton *myButton = (UIButton *)sender;

    NSMutableArray *array = [_tableArray objectAtIndex:(myButton.tag % 100)];

    FirstViewController *firstVController = [[FirstViewController alloc] init];

    firstVController.array = array;

    firstVController.tag = myButton.tag;

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

    [firstVController release];

}


#pragma mark -

#pragma mark 跳转进入页面详情

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

{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    DetailViewController *detail = [[DetailViewController alloc] init];

    NSMutableArray *first = [_tableArray objectAtIndex:indexPath.section];

    CarModel *car = [first objectAtIndex:indexPath.row];

    detail.carModel = car;

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

    [detail release];

}


#pragma mark - 

#pragma mark 代理传值方法

- (void)addTableViewCell:(CarModel *)carModel

{

    if (carModel.carName != nil) {

    int i = (int)carModel.tag % 10;

    NSMutableArray *firstt = [[[_tableArray objectAtIndex:i] retain] autorelease];

    [firstt addObject:carModel];

    [_tableArray removeObjectAtIndex:i];

    [_tableArray insertObject:firstt atIndex:i];

    [_table reloadData];

    NSLog(@"ok");

    }

    

}


@end


/*****************************/

#import <UIKit/UIKit.h>

@class CarModel;

@interface DetailViewController : UIViewController


@property (nonatomic, retain) CarModel *carModel;


@end


#import "DetailViewController.h"

#import "CarModel.h"

#import "FirstViewController.h"

@interface DetailViewController ()


@end


@implementation DetailViewController


- (void)dealloc

{

    [_carModel release];

    

    [super dealloc];

}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

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

    if (self) {

        

    }

    return self;

}


- (void)createTableView

{

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 80, 180, 200)];

    [self.view addSubview:imageView];

    [imageView release];

    [imageView setImage:[UIImage imageNamed:self.carModel.carImage]];

    

    UILabel *carLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 80, 80, 50)];

    carLabel.text = _carModel.carName;

    [self.view addSubview:carLabel];

    [carLabel release];

    

    UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, 180, 80, 50)];

    priceLabel.text = _carModel.carPrice;

    [self.view addSubview:priceLabel];

    [priceLabel release];

    

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 350, 290, 300)];

    [self.view addSubview:scroll];

    [scroll release];

    [scroll setBackgroundColor:[UIColor yellowColor]];

    

    UILabel *deLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 290, 50)];

    deLabel.text = _carModel.carDetail;

    deLabel.numberOfLines = 0;

    CGSize textSize = [deLabel.text boundingRectWithSize:CGSizeMake(290, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]} context:nil].size;

    [deLabel setFrame:CGRectMake(0, 0, 290, textSize.height)];

    deLabel.font = [UIFont systemFontOfSize:20];

    [scroll addSubview:deLabel];

    [deLabel release];

    

    [scroll setContentSize:CGSizeMake(0, deLabel.frame.size.height)];

    

}


- (void)loadView

{

    [super loadView];

    

    [self createTableView];

}




- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    [self.view setBackgroundColor:[UIColor whiteColor]];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, assign) NSInteger tag;

@end


#import "FirstViewController.h"

#import "CarModel.h"

@interface FirstViewController () <UITableViewDataSource, UITableViewDelegate>


@end


@implementation FirstViewController


- (void)dealloc

{

    [_array release];

    

    [super dealloc];

}


- (void)loadView

{

    [super loadView];

    

    [self createInterface];

    [self.view setBackgroundColor:[UIColor whiteColor]];

    self.automaticallyAdjustsScrollViewInsets = NO;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

}



- (void)createInterface

{

    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];

    [self.view addSubview:table];

    [table setDataSource:self];

    [table setDelegate:self];

    [table release];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


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

{

    return [_array count];

}


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

{

    static NSString *cellIdentify = @"cellIdentify";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];

    }

    CarModel *carM = [_array objectAtIndex:indexPath.row];

    cell.textLabel.text = carM.carName;

    [cell.imageView setImage:[UIImage imageNamed:carM.carImage]];

    

    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)sectio

{

    return 50.0f;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

{

    

    UILabel *carLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];

    carLabel.textAlignment = NSTextAlignmentCenter;

    if (_tag == 100) {

        carLabel.text = @"宝马X系列";

        

    }else if(_tag == 101){

        carLabel.text = @"宝马其他系列";

    }

    NSLog(@"====================");

    return [carLabel autorelease];

}


@end


#import <UIKit/UIKit.h>

@class CarModel;

@protocol sendTableViewCell <NSObject>


- (void)addTableViewCell:(CarModel *)carModel;


@end


@interface SecondViewController : UIViewController


//@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) CarModel *car;

@property (nonatomic, assign) id <sendTableViewCell> delegate;

@end



#import "SecondViewController.h"

#import "CarModel.h"


@interface SecondViewController () <UITextFieldDelegate>

{

    UITextField *_contentField;

}

@end


@implementation SecondViewController


- (void)dealloc

{

    [_car release];

    

    [super dealloc];

}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

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

    if (self) {

        self.car = [[CarModel alloc] init];

    }

    return self;

}


- (void)loadView

{

    [super loadView];

    [self create];

}


- (void)viewDidLoad {

    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(turnUp)];

}


- (void)turnUp

{

    if ([self.delegate respondsToSelector:@selector(addTableViewCell:)]) {

        [self.delegate addTableViewCell:_car];

    }

    [self.navigationController popViewControllerAnimated:YES];

}


- (void)create

{

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

    label.text = @"请输入想要添加的汽车名称:";

    [self.view addSubview:label];

    [label release];

    [label.layer setBorderWidth:0.7];

    [label.layer setBorderColor:[[UIColor blackColor] CGColor]];

    

    _contentField = [[UITextField alloc] initWithFrame:CGRectMake(240, 80, 100, 30)];

    [self.view addSubview:_contentField];

    [_contentField release];

    [_contentField setBackgroundColor:[UIColor darkGrayColor]];

    [_contentField setDelegate:self];

    [_contentField.layer setBorderColor:[[UIColor blackColor] CGColor]];

    [_contentField.layer setBorderWidth:0.7];

    

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button1 addTarget:self action:@selector(btn1:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button1];

    [button1 setTitle:@"宝马X系列" forState:UIControlStateNormal];

    [button1 setFrame:CGRectMake(50, 200, 100, 30)];

    [button1 setBackgroundColor:[UIColor yellowColor]];

    [button1 setTag:10];

    

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button2 addTarget:self action:@selector(btn2:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:button2];

    [button2 setTitle:@"宝马其他系列" forState:UIControlStateNormal];

    [button2 setFrame:CGRectMake(180, 200, 100, 30)];

    [button2 setBackgroundColor:[UIColor blackColor]];

    [button2 setTag:11];

    

}


- (void)btn1:(UIButton *)sender

{

    NSString *imageName = [NSString stringWithFormat:@"%d.jpg", arc4random() % 19];

    _car.carImage = imageName;

    _car.tag = sender.tag;

    _car.carName = _contentField.text;

    NSLog(@"%@, %@", _car.carName, _car.carImage);


}


- (void)btn2:(UIButton *)sender

{

    NSString *imageName = [NSString stringWithFormat:@"%d.jpg", arc4random() % 19];

    _car.carImage = imageName;

    _car.tag = sender.tag;

    _car.carName = _contentField.text;

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    self.car.carName = textField.text;

    [textField resignFirstResponder];

    return YES;

}


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    _contentField = textField;

}


@end




小型的汽车浏览界面(目前功能比较简单)

原文:http://blog.csdn.net/anders_wangw/article/details/44587175

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!