IOS开发基础之团购案例17-xib和UITableView两种方式实现
生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之团购案例17-xib和UITableView两种方式实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS開發(fā)基礎(chǔ)之團(tuán)購(gòu)案例17-xib和UITableView兩種方式實(shí)現(xiàn)
Design By Johnson Shanghai
實(shí)現(xiàn)效果
系統(tǒng)和Xcode版本
注意的細(xì)節(jié)
關(guān)鍵性的代碼
// // ViewController.m // 17-團(tuán)購(gòu)案例 // // 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:@"¥ %@ %@人已購(gòu)買",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-團(tuán)購(gòu)案例 // // 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;@endNS_ASSUME_NONNULL_END // // CZGoods.m // 17-團(tuán)購(gòu)案例 // // 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-團(tuán)購(gòu)案例 // // 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;@endNS_ASSUME_NONNULL_END // // CZGoodsCell.m // 17-團(tuán)購(gòu)案例 // // Created by 魯軍 on 2021/2/4. //#import "CZGoodsCell.h" #import "CZGoods.h" //類擴(kuò)展 @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:@"%@ 人已購(gòu)買",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總結(jié)
以上是生活随笔為你收集整理的IOS开发基础之团购案例17-xib和UITableView两种方式实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 阶乘的精确值 大数问题
- 下一篇: 银行不告诉的秘密,看完豁然大悟