IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
生活随笔
收集整理的這篇文章主要介紹了
IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
******HMTg.h 模型數據?
#import <Foundation/Foundation.h>@interface HMTg : 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******HMTg.m?模型數據?
#import "HMTg.h"@implementation HMTg- (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*********cell的文件HMTgCell.h
#import <UIKit/UIKit.h>@interface HMTgCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleLabel; @property (weak, nonatomic) IBOutlet UILabel *priceLabel; @property (weak, nonatomic) IBOutlet UILabel *buyCountLabel; @end****HMTgCell.m
#import "HMTgCell.h"@interface HMTgCell()@end@implementation HMTgCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {// Initialization code }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**********HMViewController.m
#import "HMViewController.h" #import "HMTg.h" #import "HMTgCell.h"@interface HMViewController () @property (nonatomic, strong) NSArray *tgs; @end@implementation HMViewController- (NSArray *)tgs {if (_tgs == nil) _tgs = [HMTg tgs];return _tgs; }- (void)viewDidLoad {[super viewDidLoad];self.tableView.rowHeight = 80;// 調整邊距,可以讓表格視圖讓開狀態欄self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0); }///** 隱藏狀態欄 */ //- (BOOL)prefersStatusBarHidden //{ // return YES; //}#pragma mark - 數據源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.tgs.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {// 1. 可重用標示符static NSString *ID = @"Cell";// 2. tableView查詢可重用CellHMTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 3. 如果沒有可重用cellif (cell == nil) { // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];// 從XIB加載自定義視圖 cell = [[[NSBundle mainBundle] loadNibNamed:@"HMTgCell" owner:nil options:nil] lastObject];}// 4. 設置Cell內容// 1> 取出模型HMTg *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];cell.titleLabel.text = tg.title;cell.iconView.image = [UIImage imageNamed:tg.icon];cell.priceLabel.text = tg.price;cell.buyCountLabel.text = tg.buyCount;return cell; }@end?
轉載于:https://www.cnblogs.com/ios-g/p/4710977.html
總結
以上是生活随笔為你收集整理的IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 参数估计:最大似然、贝叶斯与最大后验
- 下一篇: 循环数组的最大子段和