zoukankan      html  css  js  c++  java
  • ios-用xib和UI table View controller 的团购网站

    首先 素材 照例是照片和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
  • 相关阅读:
    C语言经典算法100例-039-排序队列中插入新元素
    C语言经典算法100例-037-给10个数排序
    C语言经典算法100例-036-求100之内的素数
    C语言经典算法100例-032~35
    C语言经典算法100例-031-判断星期几
    安卓 短信页面设置(线性布局)
    CSS样式表
    HTML表单
    第1部分 HTML 表格
    多线程
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4739438.html
Copyright © 2011-2022 走看看