IOS开发基础之团购案例17-xib和UITableView两种方式实现

IOS开发基础之团购案例17-xib和UITableView两种方式实现

Design By Johnson Shanghai
实现效果

在这里插入图片描述

在这里插入图片描述

系统和Xcode版本

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

注意的细节

在这里插入图片描述

关键性的代码

//
//  ViewController.m
//  17-团购案例
//
//  Created by 鲁军 on 2021/2/4.
//

#import "ViewController.h"
#import "CZGoods.h"
#import "CZGoodsCell.h"

@interface ViewController ()<UITableViewDataSource>

@property(nonatomic,strong)NSArray *goods;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CZGoods *model = self.goods[indexPath.row];
    
    
//    static Nsstring *ID= @"goods_cell";
//
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//    if(cell==nil){
//
//        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
//
//    }
//    cell.imageView.image = [UIImage imageNamed:model.icon];
//    cell.textLabel.text = model.title
//    ;
//    cell.detailTextLabel.text = [Nsstring stringWithFormat:@"¥ %@      %@人已购买",model.price,model.buyCount];
    
    CZGoodsCell *cell = [CZGoodsCell goodsCellWithTableView:tableView];
    
    
    
    cell.goods = model;
    
    
    
    
  
    
    
    
    return cell;
    
}


- (BOOL)prefeRSStatusBarHidden{
    return YES;
}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return  1;
//}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.goods.count;
    
}

- (NSArray *)goods{
    
    if(_goods==nil){
        
        Nsstring *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
        NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *attayModles = [NSMutableArray array];
        for(NSDictionary *dict in arrayDict){
            CZGoods *model = [CZGoods goodsWithDict:dict];
            [attayModles addobject:model];
        }
        _goods = attayModles;
        
    }
    
    return _goods;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView.rowHeight = 60;
    
    
}


@end

//
//  CZGoods.h
//  17-团购案例
//
//  Created by 鲁军 on 2021/2/4.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface CZGoods : NSObject

@property(nonatomic,copy)Nsstring *buyCount;
@property(nonatomic,copy)Nsstring *price;
@property(nonatomic,copy)Nsstring *title;
@property(nonatomic,copy)Nsstring *icon;

-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)goodsWithDict:(NSDictionary *)dict;


@end

NS_ASSUME_NONNULL_END

//
//  CZGoods.m
//  17-团购案例
//
//  Created by 鲁军 on 2021/2/4.
//

#import "CZGoods.h"

@implementation CZGoods
-(instancetype)initWithDict:(NSDictionary *)dict{
    if(self=[super init]){
        [self setValuesForKeysWithDictionary:dict];
        
        
    }
    
    return self;
}
+(instancetype)goodsWithDict:(NSDictionary *)dict{
    return [[self alloc] initWithDict:dict];
}
@end

//
//  CZGoodsCell.h
//  17-团购案例
//
//  Created by 鲁军 on 2021/2/4.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@class CZGoods;
@interface CZGoodsCell : UITableViewCell


@property(nonatomic,strong)CZGoods *goods;


+(instancetype)goodsCellWithTableView:(UITableView *)tableView;


@end

NS_ASSUME_NONNULL_END

//
//  CZGoodsCell.m
//  17-团购案例
//
//  Created by 鲁军 on 2021/2/4.
//

#import "CZGoodsCell.h"
#import "CZGoods.h"
//类扩展
@interface CZGoodsCell()
@property (weak, nonatomic) IBOutlet UIImageView *imgViewIcon;

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

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

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

@end

@implementation CZGoodsCell
+ (instancetype)goodsCellWithTableView:(UITableView *)tableView{
    static Nsstring *ID= @"goods_cell";
    CZGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell==nil){
        cell=[[[NSBundle mainBundle] loadNibNamed:@"CZGoodsCell" owner:nil options:nil] firstObject];
        
    }
    return cell;
}




- (void)setGoods:(CZGoods *)goods{
    _goods = goods;
    
    self.imgViewIcon.image =[UIImage imageNamed:goods.icon];
    self.lblTitle.text=goods.title;
    self.lblPrice.text=[Nsstring stringWithFormat:@"¥ %@",goods.price];
    self.lblBuyCount.text=[Nsstring stringWithFormat:@"%@ 人已购买",goods.buyCount];
    
    
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

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

    // Configure the view for the selected state
}

@end

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...