當(dāng)前位置:
首頁(yè) >
ios第三方数据请求 UI_15
發(fā)布時(shí)間:2023/12/20
44
豆豆
生活随笔
收集整理的這篇文章主要介紹了
ios第三方数据请求 UI_15
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
AppDelegate.m
//指定根視圖self.window.rootViewController = [[[UINavigationController alloc]initWithRootViewController:[HomeViewController new]]autorelease];
自定義cell文件:
NewsCell.h
#import <UIKit/UIKit.h> @class News; @interface NewsCell : UITableViewCell //寫(xiě)一個(gè)方法給cell上的控件賦值 - (void)assiginValueByNews : (News *)news; //定義一個(gè)類方法返回cell的行高 //根據(jù)傳進(jìn)來(lái)的數(shù)據(jù),計(jì)算當(dāng)前cell的行高 + (CGFloat)cellHeight : (News *)news; @end
NewsCell.m
#import "News.h" #import "UIImageView+WebCache.h" @interface NewsCell () @property(nonatomic,retain)UIImageView *picView; @property(nonatomic,retain)UILabel *titleLabel; @property(nonatomic,retain)UILabel *summaryLabel;@end@implementation NewsCell - (void)dealloc{self.picView = nil;self.titleLabel = nil;self.summaryLabel = nil;[super dealloc]; }- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {[self.contentView addSubview:self.titleLabel];[self.contentView addSubview:self.summaryLabel];[self.contentView addSubview:self.picView];}return self;}//懶加載 //picView - (UIImageView *)picView{if (_picView == nil) {self.picView = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 5, 80, 90)]autorelease]; // self.picView.backgroundColor = [UIColor orangeColor];}return [[_picView retain]autorelease]; }//titleLabel - (UILabel *)titleLabel{if (_titleLabel == nil) {self.titleLabel = [[[UILabel alloc]initWithFrame:CGRectMake(80, 5, 250, 30)]autorelease];self.titleLabel.backgroundColor = [UIColor orangeColor];//設(shè)置文字大小self.titleLabel.font = [UIFont systemFontOfSize:17.0];//根據(jù)內(nèi)容換行self.titleLabel.numberOfLines = 0;}return [[_titleLabel retain]autorelease]; }//summmaryLabel - (UILabel *)summaryLabel{if (_summaryLabel == nil) {self.summaryLabel = [[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 305, 55)]autorelease]; // self.summaryLabel.backgroundColor = [UIColor cyanColor];//設(shè)置文字大小self.summaryLabel.font = [UIFont systemFontOfSize:17.0];//根據(jù)內(nèi)容換行self.summaryLabel.numberOfLines = 0;}return [[_summaryLabel retain]autorelease];}//寫(xiě)一個(gè)方法給cell上的控件賦值 - (void)assiginValueByNews : (News *)news{//1.使用圖片異步加載的方法添加圖片,此時(shí)使用SDWebImage第三方,先加載一張默認(rèn)圖片作為占位符,等從網(wǎng)上請(qǐng)求下來(lái)數(shù)據(jù)的時(shí)候再賦值給控件[self.picView sd_setImageWithURL:[NSURL URLWithString:news.hot_pic]placeholderImage:[UIImage imageNamed:@"1.jpg"]]; // self.imageView.image = [UIImage imageNamed:@"1.jpg"];self.titleLabel.text = news.title;self.summaryLabel.text = news.summary;//summaryLabel//修改完成之后重新計(jì)算self.summaryLabel的大小CGRect summaryRecct = self.summaryLabel.frame;//修改summaryRect的高summaryRecct.size.height = [[self class]summaryLabelHeight:news.summary];//將修改過(guò)后的大小賦值給self.summaryLabel.frameself.summaryLabel.frame = summaryRecct;//titieLabel//修改完成之后重新計(jì)算self.titleLabel的大小CGRect titleRect = self.titleLabel.frame;//修改它的高titleRect.size.height = [[self class]titleLabelHeight:news.title];//將修改過(guò)后的大小賦值給self.titleLabel.frameself.titleLabel.frame = titleRect;} //title + (CGFloat)titleLabelHeight : (NSString *)title{CGSize contextSize = CGSizeMake(250, 0);//設(shè)置計(jì)算時(shí)文本的一些屬性,比如:字體的大小NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:17.0]};CGRect titleRect = [title boundingRectWithSize:contextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];return titleRect.size.height;}//summary + (CGFloat)summaryLabelHeight : (NSString *)summary{CGSize contextSize = CGSizeMake(305, 0);//設(shè)置計(jì)算時(shí)文本的一些屬性,比如:字體的大小NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:17.0]};CGRect summaryRect = [summary boundingRectWithSize:contextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];return summaryRect.size.height;}//定義一個(gè)類方法返回cell的行高 //根據(jù)傳進(jìn)來(lái)的數(shù)據(jù),計(jì)算當(dāng)前cell的行高 + (CGFloat)cellHeight : (News *)news{CGFloat summaryHeight = [self summaryLabelHeight:news.summary];CGFloat titleHeight = [self titleLabelHeight:news.title];return 5 + 30 + 10 + 10 +30 +summaryHeight + titleHeight; }model數(shù)據(jù)類型文件:
News.h
#import <Foundation/Foundation.h>@interface News : NSObject @property(nonatomic,copy)NSString *title;//標(biāo)題 @property(nonatomic,copy)NSString *hot_pic;//圖片 @property(nonatomic,copy)NSString *summary;//新聞內(nèi)容@end News.m@implementation News- (void)dealloc{self.title = nil;self.summary = nil;self.hot_pic = nil;[super dealloc];}- (void)setValue:(id)value forUndefinedKey:(NSString *)key{//碰到key值是description 時(shí)候,將value賦值給summaryif ([key isEqualToString:@"description"]) {self.summary = value;} }@end開(kāi)始使用第三方數(shù)據(jù)請(qǐng)求:
HomeViewController.m
#import "NewsCell.h" #import "AFNetworking.h" #import "News.h" #define kNewsCell @"news-cell" @interface HomeViewController () @property(nonatomic,retain)NSMutableArray *dataSource; @end@implementation HomeViewController- (void)viewDidLoad {[super viewDidLoad];self.dataSource = nil;self.title = @"新聞";[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"3"] forBarMetrics:UIBarMetricsDefault];//注冊(cè)[self.tableView registerClass:[NewsCell class] forCellReuseIdentifier:kNewsCell];//調(diào)用從網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)[self readDataFormNetWork];} //懶加載 - (NSMutableArray *)dataSource{if (_dataSource == nil) {self.dataSource = [NSMutableArray arrayWithCapacity:0];}return [[_dataSource retain]autorelease]; }//從網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù) - (void)readDataFormNetWork{//1.準(zhǔn)備網(wǎng)址對(duì)象NSString *urlStr = @"http://www.bjnews.com.cn/api/get_hotlist.php?page=1";//2.使用第三方AFNetWorking,做網(wǎng)絡(luò)請(qǐng)求,現(xiàn)在是一種主流的網(wǎng)絡(luò)請(qǐng)求方式//如果導(dǎo)入的第三方文件不支持MRC工程環(huán)境,選中target-->Bulid phases -->complie sources 將對(duì)應(yīng)的文件后加入 -fobjc-arc//3.創(chuàng)建請(qǐng)求管理者AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];//4.設(shè)置支持的數(shù)據(jù)格式manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//5.請(qǐng)求數(shù)據(jù)__block typeof(self)weakself = self;[manger GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {//responseObject 請(qǐng)求下來(lái)的數(shù)據(jù)在這里存儲(chǔ),并且這個(gè)數(shù)據(jù)已經(jīng)解析好了 // NSLog(@"%@",responseObject);NSMutableArray *mArray = responseObject[@"list"];for (NSDictionary *dic in mArray) {//創(chuàng)建model對(duì)象News *news = [[News alloc]init];//給model 賦值[news setValuesForKeysWithDictionary:dic];//添加到存放所有新聞對(duì)象的數(shù)組[weakself.dataSource addObject:news];[news release];} // NSLog(@"%@",self.dataSource); 驗(yàn)證!//刷新UI界面[weakself.tableView reloadData];} failure:^(AFHTTPRequestOperation *operation, NSError *error) {//存儲(chǔ)請(qǐng)求失敗的信息}];} 顯示在cell的控件上:#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {// Return the number of sections.return 1; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {// Return the number of rows in the section.return self.dataSource.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsCell forIndexPath:indexPath];News *news = self.dataSource[indexPath.row];[cell assiginValueByNews:news];//選中cell的背景顏色cell.selectedBackgroundView = [[[UIView alloc]initWithFrame:cell.frame]autorelease];cell.selectedBackgroundView.backgroundColor = [UIColor greenColor];return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return [NewsCell cellHeight:self.dataSource[indexPath.row]]; } 素材下載: ? ?
第三方AFNetWorking、SDWebImage下載:http://pan.baidu.com/s/1FOOkm
總結(jié)
以上是生活随笔為你收集整理的ios第三方数据请求 UI_15的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IDE也卷了,微软杀入嵌入式IDE
- 下一篇: 计算机 分类号,中图法分类号(计算机,自