码迷,mamicode.com
首页 > 移动开发 > 详细

ios-用xib和UI table View controller 的团购网站

时间:2015-08-18 16:15:49      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

首先 素材 照例是照片和plist文件 然后依旧是 字典转模型

UI table View controller 的缺点是 不能灵活控制 图片的大小 和布局

所以 新建一个xib文件,然后新建同名的类  xib继承此类 里面的控件可以拖线到类中 

//
//  Tg.h
//  团购网
//
//  Created by YaguangZhu on 15/8/18.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Tg : NSObject
@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *icon;
@property(nonatomic,copy) NSString *price;
@property(nonatomic,copy) NSString *buyCount;

- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)tgWithDict:(NSDictionary *)dict;
+ (NSArray *)tgs;

@end
//
//  Tg.m
//  团购网
//
//  Created by YaguangZhu on 15/8/18.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "Tg.h"

@implementation Tg

- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (instancetype)tgWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

+ (NSArray *)tgs
{
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]];
    NSMutableArray *arrayM = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        [arrayM addObject:[self tgWithDict:dict]];
    }
    
    return arrayM;
}

@end
//
//  ViewController.m
//  团购网
//
//  Created by YaguangZhu on 15/8/18.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "ViewController.h"
#import "Tg.h"
#import "TgCell.h"
@interface ViewController ()

@property(nonatomic,strong) NSArray *tgs;
@end

@implementation ViewController

- (NSArray *)tgs
{
    if (_tgs ==nil) {
        _tgs  = [Tg tgs];
    }
    return _tgs;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 80;
    
    //调整边距 实现状态栏的干扰
    // 调整边距,可以让表格视图让开状态栏
    self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    // Do any additional setup after loading the view, typically from a nib.
}

//隐藏状态栏
//- (BOOL)prefersStatusBarHidden
//{
//    return YES;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    TgCell *cell1 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    
    if (cell1 == nil) {
        //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        
        //从xib里面加载自定义视图
        cell1 = [[[NSBundle mainBundle] loadNibNamed:@"TgCell" owner:nil options:nil] lastObject];
    }
    
    Tg *tg = self.tgs[indexPath.row];
//    cell.textLabel.text = tg.title;
//    cell.imageView.image = [UIImage imageNamed:tg.icon];
//    cell.detailTextLabel.text = [NSString stringWithFormat:@"$ %@          已有%@人购买",tg.price,tg.buyCount];
    
    cell1.titleLabel.text = tg.title;
    cell1.imageView.image = [UIImage imageNamed:tg.icon];
    cell1.priceLabel.text = tg.price;
    cell1.buyCountLabel.text = tg.buyCount;
    
    
    return cell1;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
//
//  TgCell.h
//  团购网
//
//  Created by YaguangZhu on 15/8/18.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TgCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;




@end
//
//  TgCell.m
//  团购网
//
//  Created by YaguangZhu on 15/8/18.
//  Copyright (c) 2015年 YaguangZhu. All rights reserved.
//

#import "TgCell.h"


@implementation TgCell



- (void)awakeFromNib {
    // Initialization code
}

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

    // Configure the view for the selected state
}

@end

 

ios-用xib和UI table View controller 的团购网站

标签:

原文地址:http://www.cnblogs.com/zhuyaguang/p/4739438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!