<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

简介: AppDelegate.m //指定根视图 self.window.rootViewController = [[[UINavigationController alloc]initWit...

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


目录
相关文章
|
Web App开发 前端开发
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
 Connection reset by peer的常见原因: 1)服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭;    如果知道实际连接服务器的并发客户数没有超过服务器的承载量,看下有没有网络流量异常。
827 0
|
Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
深度分析Java的ClassLoader机制(源码级别) 写在前面:Java中的所有类,必须被装载到jvm中才能运行,这个装载工作是由jvm中的类装载器完成的,类装载器所做的工作实质是把类文件从硬盘读取到内存中,JVM在加载类的时候,都是通过ClassLoader的loadClass()方法来加载class的,loadClass使用双亲委派模式。
1044 0
|
Web App开发 Java Apache
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一个典型的星型模式包括一个大型的事实表和一组逻辑上围绕这个事实表的维度表。  事实表是星型模型的核心,事实表由主键和度量数据两部分组成。
511 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
zookeeper的maxSessionTimeout默认值导致hbase regionserver超时 在hbase中经常会遇到regionserver挂掉的情况,查看日志会看到这样的错误信息 2016-02-16 11:51:24,882 WARN  [master/hadoop02/192.
721 0
|
Web App开发 前端开发 大数据
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
提  纲     1、移动DSP与传统营销有什么不同?     2、为什么移动DSP是大势所趋?     3、哪些因素决定移动DSP的精准与否?     4、如何辨别移动DSP的真伪优劣?     ...
928 0
|
Web App开发 监控 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
最近在监控中发现HiveServer2连接到zookeeper里的连接持续上涨,很奇怪,虽然知道HiveServer2支持并发连接,使用ZooKeeper来管理Hive表的读写锁,但我们的环境并不需要这些,我们已经关闭并发功能,以下是线上的配置,甚至把这些值都改成final了。
708 0
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
在hadoop测试集群运行job的过程中发现部分运行失败,有Cannot obtain block length for LocatedBlock,使用hdfs dfs -cat ${文件}的时候也报这个错,看过代码后发现...
673 0