AppDelegate.m

    //指定根视图
    self.window.rootViewController = [[[UINavigationController alloc]initWithRootViewController:[HomeViewController new]]autorelease];

自定义cell文件:

NewsCell.h

#import <UIKit/UIKit.h>
@class News;
@interface NewsCell : UITableViewCell
//写一个方法给cell上的控件赋值
- (void)assiginValueByNews : (News *)news;
//定义一个类方法返回cell的行高
//根据传进来的数据,计算当前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];
        //设置文字大小
        self.titleLabel.font = [UIFont systemFontOfSize:17.0];
        //根据内容换行
        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];
        //设置文字大小
        self.summaryLabel.font = [UIFont systemFontOfSize:17.0];
        //根据内容换行
        self.summaryLabel.numberOfLines = 0;

    }
    return [[_summaryLabel retain]autorelease];

}

//写一个方法给cell上的控件赋值
- (void)assiginValueByNews : (News *)news{
    //1.使用图片异步加载的方法添加图片,此时使用SDWebImage第三方,先加载一张默认图片作为占位符,等从网上请求下来数据的时候再赋值给控件
    [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
    //修改完成之后重新计算self.summaryLabel的大小
    CGRect summaryRecct = self.summaryLabel.frame;
    //修改summaryRect的高
    summaryRecct.size.height = [[self class]summaryLabelHeight:news.summary];
    //将修改过后的大小赋值给self.summaryLabel.frame
    self.summaryLabel.frame = summaryRecct;

    //titieLabel
    //修改完成之后重新计算self.titleLabel的大小
    CGRect titleRect = self.titleLabel.frame;
    //修改它的高
    titleRect.size.height = [[self class]titleLabelHeight:news.title];
    //将修改过后的大小赋值给self.titleLabel.frame
    self.titleLabel.frame = titleRect;

}
//title
+ (CGFloat)titleLabelHeight : (NSString *)title{

    CGSize contextSize = CGSizeMake(250, 0);
      //设置计算时文本的一些属性,比如:字体的大小
    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);
    //设置计算时文本的一些属性,比如:字体的大小
    NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:17.0]};
    CGRect summaryRect = [summary boundingRectWithSize:contextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return summaryRect.size.height;

}

//定义一个类方法返回cell的行高
//根据传进来的数据,计算当前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数据类型文件:

News.h

#import <Foundation/Foundation.h>

@interface News : NSObject
@property(nonatomic,copy)NSString *title;//标题
@property(nonatomic,copy)NSString *hot_pic;//图片
@property(nonatomic,copy)NSString *summary;//新闻内容

@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 时候,将value赋值给summary
    if ([key isEqualToString:@"description"]) {
        self.summary = value;
    }
}

@end

开始使用第三方数据请求:

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];

    //注册
    [self.tableView registerClass:[NewsCell class] forCellReuseIdentifier:kNewsCell];
    //调用从网络请求数据
    [self readDataFormNetWork];

}
//懒加载
- (NSMutableArray *)dataSource{
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];

    }
    return [[_dataSource retain]autorelease];
}

//从网络请求数据
- (void)readDataFormNetWork{
    //1.准备网址对象
    NSString *urlStr = @"http://www.bjnews.com.cn/api/get_hotlist.php?page=1";
    //2.使用第三方AFNetWorking,做网络请求,现在是一种主流的网络请求方式
    //如果导入的第三方文件不支持MRC工程环境,选中target-->Bulid phases -->complie sources 将对应的文件后加入 -fobjc-arc
    //3.创建请求管理者
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];

    //4.设置支持的数据格式
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    //5.请求数据

    __block typeof(self)weakself = self;

    [manger GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //responseObject  请求下来的数据在这里存储,并且这个数据已经解析好了
//        NSLog(@"%@",responseObject);

        NSMutableArray *mArray = responseObject[@"list"];

        for (NSDictionary *dic in mArray) {
            //创建model对象
            News *news = [[News alloc]init];
            //给model 赋值
            [news setValuesForKeysWithDictionary:dic];
            //添加到存放所有新闻对象的数组
            [weakself.dataSource addObject:news];
            [news release];

        }
//        NSLog(@"%@",self.dataSource); 验证!
        //刷新UI界面
        [weakself.tableView reloadData];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //存储请求失败的信息

    }];

}

显示在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

ios第三方数据请求 UI_15的更多相关文章

  1. iOS之数据请求NSURLConnection

    iOS之数据请求NSURLConnection NSString *lcsUrl = @"http://192.168.1.1:8080/lcsUrl"; //假设网址中有汉字.须 ...

  2. iOS服务器数据请求"汉字编码"问题

    下面记录一下数据请求问题: 1.不知道大家有木有遇到过,当数据请求的URL带有汉字的时候,请求数据肯定会报404错误,也就是参数或者是接口URL错误<虽然说404,500等错误一般都是服务器问题 ...

  3. iOS - NetRequest 网络数据请求

    1.网络请求 1.1 网络通讯三要素 1.IP 地址(主机名): 网络中设备的唯一标示.不易记忆,可以用主机名(域名). 1) IP V4: 0~255.0~255.0~255.0~255 ,共有 2 ...

  4. iOS开发——网络Swift篇&NSURL进行数据请求(POST与GET)

    NSURL进行数据请求(POST与GET)   使用Swift进行iOS开发时,不可避免的要进行远程的数据获取和提交. 其数据请求的方式既可能是POST也可能是GET.同不管是POST还是GET又可以 ...

  5. iOS中GET 和 POST 数据请求

    iOS中GET 和 POST 网络数据请求 同步请求和异步请求的差别: 1.同步请求,有主线程完成网路请求任务,在数据没有请求之前,用户的所有的交互事件应用都无法处理,会造成一种卡顿现象,影响用户体验 ...

  6. iOS第三方Api及常用框架总结

    iOS常用框架汇总: SVProgressHUD:产生覆盖层,禁止某种操作 SDWebImage: 专业下载图片框架 AFN:网络数据请求框架 MJExtension,模型对象之间互转 第三方分享第三 ...

  7. 使用 AFNetworking 进行 XML 和 JSON 数据请求

    (1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...

  8. iOS - Alamofire 网络请求

    前言 Alamofire 是 Swift 语言的 HTTP 网络开发工具包,相当于 Swift 实现 AFNetworking 版本.当然,AFNetworking 非常稳定,在 Mac OSX 与 ...

  9. iOS - AFNetworking 网络请求

    前言 在 iOS 开发中,一般情况下,简单的向某个 Web 站点简单的页面提交请求并获取服务器的响应,用 Xcode 自带的 NSURLConnection 是能胜任的.但是,在绝大部分下我们所需要访 ...

随机推荐

  1. Ubuntu16.04下安装jdk1.8过程

    笔者环境:腾讯云服务器 Ubuntu16.04 x64 一 . 去oracle官网下载对应的jdk 下载地址:http://www.oracle.com/technetwork/java/javase ...

  2. ES6(es2015)新增实用方法汇总

    Array 1.map() [1,2,3,4].map(function(item, index, array){ return  item * 2; }) 对数组中的每一项执行一次回调函数,三个参数 ...

  3. Java 读取Excel文件

    https://www.cnblogs.com/wwzyy/p/5962076.html   先把上面的参考博客看了,如果会导入包的话,下面的教程就直接忽略emm     这时候,你应该把jar包下载 ...

  4. Node.js 进程

    process 是全局对象,能够在任意位置访问,是 EventEmitter 的实例. 退出状态码 当没有新的异步的操作等待处理时,Node 正常情况下退出时会返回状态码 0 .下面的状态码表示其他状 ...

  5. Bootstrap3 代码-程序输出

    通过 <samp> 标签来标记程序输出的内容. This text is meant to be treated as sample output from a computer prog ...

  6. linux系统性能监控--I/O利用率

    尽管整体的处理器速度. 内存大小以及 I/O执行速度在不断提高,但 I/O操作的吞吐率和延迟性能仍然要比等价的内存访问操作低多个数量级.另外,由于许多工作负荷都拥有重要的I/O组件,I/O处理很容易成 ...

  7. 潜谈IT从业人员在传统IT和互联网之间的择业问题(下)-互联网公司

    互联网带来的一片晴天 相对于传统行业来说,互联网行业要显得相对对技术人员尊重些. 在互联网行业中,采用的技术.概念也较传统形行业来说要新,技术人员也容易在此找到自己的一方净土. 因为互联网这个行当讲究 ...

  8. 序列化战争:主流序列化框架Benchmark

    序列化战争:主流序列化框架Benchmark GitHub上有这样一个关于序列化的Benchmark,被好多文章引用.但这个项目考虑到完整性,代码有些复杂.为了个人学习,自己实现了个简单的Benchm ...

  9. Dynamics CRM 打开数据加密报错及修改用户邮件保存报错的解决方法

    在项目里会碰到在修改用户的电子邮件时报错的问题 然后跑到数据管理里打开数据加密又是报错 解决上述问题只需要做下数据库的更改即可,把标志位置1即可,记得要重启下IIS才能生效 SELECT [Colum ...

  10. Dynamics CRM 产品视图列上自带按钮的隐藏

    CRM中对command bar的处理都是使用ribbon workbench,但是很多系统自带的按钮你是没法在ribbon workbench看到的,咱们以产品为例,比如我要隐藏form上的保存按钮 ...