//
// QiushiListViewController.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright ©
. All rights reserved.
//
#import
<UIKit/UIKit.h>
@interface
QiushiListViewController :
UITableViewController
@end
//
// QiushiListViewController.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright
. All rights reserved.
//
#import
"QiushiListViewController.h"
#import
"UIKit+AFNetworking.h"
#import
"QiushiModel.h"
#import
"AFNetworking.h"
#import
"QiushiItemCell.h"
@interface
QiushiListViewController
()
//
根据页码请求对应数据的方法
- (
void
)acquireServerDataWithPageNumber:(
NSInteger
)page;
@property
(
nonatomic
,
retain
)
NSMutableArray
*datasource;
@property
(
nonatomic
,
assign
)
NSInteger
page;
//
保存当前已经下载的页码
@end
@implementation
QiushiListViewController
- (
void
)dealloc
{
[_datasource release];
[
super
dealloc ];
}
- (NSMutableArray *)datasource{
if
( !_datasource ) {
self
.datasource = [NSMutableArray array];
}
return
_datasource;
}
//
实现自定义方法
- (
void
)acquireServerDataWithPageNumber:(NSInteger)page{
NSString *URLString = [NSString stringWithFormat:
@"http://m2.qiushibaike.com/article/list/text?count=30&readarticles=%%5B113067229%%2C113049786%%2C113049938%%5D&page=%ld&AdID=1443425077789327F1E73A"
, page];
NSLog(
@"%@"
, URLString);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:URLString parameters:
nil
success:^(AFHTTPRequestOperation *
_Nonnull
operation,
id
_Nonnull
responseObject) {
// NSLog( @"%@", operation);
//
当数据下载完成是结束刷新
[
self
.refreshControl endRefreshing];
if
(
self
.page ==
1
){
//
判断如果是下拉刷新
则清空数组原有对象
[
self
.datasource removeAllObjects];
}
NSArray *sourceArray = [responseObject objectForKey:
@"items"
];
for
(NSDictionary *dict
in
sourceArray) {
QiushiModel *model = [[[QiushiModel alloc]init] autorelease];
[model setValuesForKeysWithDictionary:dict];
[
self
.datasource addObject:model];
}
//
一定要在封装完成后重新加载数据
// [self.tableView reloadData];
//
让对象调用的方法延迟一定时间执行
// [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:3];
if
(
self
.page ==
1
){
[
self
.tableView reloadData];
}
else
{
[
self
.tableView performSelector:
@selector
(reloadData) withObject:
nil
afterDelay:
3
];
}
} failure:^(AFHTTPRequestOperation *
_Nonnull
operation, NSError *
_Nonnull
error) {
NSLog(
@"%@"
, error);
}];
}
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//
刷新时的动画控件
self
.refreshControl = [[[UIRefreshControl alloc]initWithFrame:CGRectZero] autorelease];
//
这是
iOS6
之后的下拉刷新控件
仅限
UITableViewController
使用
[
self
.refreshControl addTarget:
self
action:
@selector
(handleRefreshAction:) forControlEvents:UIControlEventValueChanged];
//
手动执行开始刷新动画
[
self
.refreshControl beginRefreshing];
[
self
handleRefreshAction:
self
.refreshControl];
}
//
下拉刷新动作实现方法
- (
void
)handleRefreshAction:(UIRefreshControl *)sender{
self
.page =
1
;
[
self
acquireServerDataWithPageNumber:
self
.page];
}
- (
void
)didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
return
1
;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
if
(
self
.datasource.count ==
0
) {
return
self
.datasource.count;
}
return
self
.datasource.count +
1
;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if
(indexPath.row ==
self
.datasource.count &&
self
.datasource.count >
0
) {
return
[tableView dequeueReusableCellWithIdentifier:
@"LOAD"
forIndexPath:indexPath];
}
QiushiItemCell *cell = [tableView dequeueReusableCellWithIdentifier:
@"CELL"
forIndexPath:indexPath];
// Configure the cell...
QiushiModel *model =
self
.datasource[indexPath.row];
[cell configureCellWithModel:model];
return
cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// NSString *content = model.content;
// CGRect rect = [content boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.tableView.bounds), 100000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
if
(indexPath.row ==
self
.datasource.count &&
self
.datasource.count >
0
) {
return
50
;
}
QiushiModel *model =
self
.datasource[indexPath.row];
return
model.contentHeight +
78
+
74
;
}
- (
void
)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if
(
self
.datasource.count >
0
&& indexPath.row ==
self
.datasource.count ) {
//
说明当前是最后一行数据
下拉加载下一页数据
self
.page ++;
[
self
acquireServerDataWithPageNumber:
self
.page];
}
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// FindViewController.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<UIKit/UIKit.h>
@interface
FindViewController :
UITableViewController
@end
//
// FindViewController.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"FindViewController.h"
#import
"FindModel.h"
@interface
FindViewController ()
@property
(
nonatomic
,
retain
)NSMutableArray *datasource;
@end
@implementation
FindViewController
- (
void
)dealloc
{
[_datasource release];
[
super
dealloc];
}
- (NSMutableArray *)datasource{
if
(!_datasource) {
self
.datasource = [NSMutableArray array];
}
return
_datasource;
}
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSString *sourcePath = [[NSBundle mainBundle]pathForResource:
@"Find_HardCode"
ofType:
@"plist"
];
NSArray *sourceArray = [NSArray arrayWithContentsOfFile:sourcePath];
for
(NSDictionary *dict
in
sourceArray) {
FindModel *model = [[[FindModel alloc]init] autorelease];
//
使用
KVC
为
model
的属性赋值
[model setValuesForKeysWithDictionary:dict];
[
self
.datasource addObject:model];
}
self
.tableView.rowHeight =
80
;
}
- (
void
)didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
return
1
;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
return
self
.datasource.count;
}
- (
UITableViewCell
*)tableView:(
UITableView
*)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath {
UITableViewCell
*cell = [tableView
dequeueReusableCellWithIdentifier
:
@"CELL"
forIndexPath
:indexPath];
// Configure the cell...
FindModel
*model =
self
.
datasource
[indexPath.
row
];
cell.
imageView
.
image
= [
UIImage
imageNamed
:model.
image
];
cell.
textLabel
.
text
= model.
title
;
cell.
detailTextLabel
.
text
= model.
subtitle
;
return
cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// MyselfViewController.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<UIKit/UIKit.h>
@interface
MyselfViewController :
UITableViewController
@end
//
// MyselfViewController.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"MyselfViewController.h"
#import
"MyselfModel.h"
@interface
MyselfViewController
()
@property
(
nonatomic
,
retain
)
NSMutableArray
*datasource;
@end
@implementation
MyselfViewController
- (
void
)dealloc
{
[
_datasource
release
];
[
super
dealloc
];
}
- (
NSMutableArray
*)datasource{
if
(!
_datasource
) {
self
.
datasource
= [
NSMutableArray
array
];
}
return
_datasource
;
}
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSString *sourcePath = [[NSBundle mainBundle]pathForResource:
@"Myself_hardCode"
ofType:
@"plist"
];
NSArray *sourceArray = [NSArray arrayWithContentsOfFile:sourcePath];
for
(
int
i =
0
; i < sourceArray.count; i ++) {
NSMutableArray *array = [NSMutableArray array];
[
self
.datasource addObject:array];
//
得到对下表的内层数组对象
NSArray *innerArray = sourceArray[i];
//
快速便利内层数组
for
(NSDictionary *dict
in
innerArray) {
//
创建
model
对象
MyselfModel *model = [[[MyselfModel alloc]init] autorelease];
[model setValuesForKeysWithDictionary:dict];
[array addObject:model];
}
}
}
- (
void
)didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
return
self
.datasource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
return
[
self
.datasource[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = indexPath.section ==
2
&& indexPath.row ==
0
?
@"SWITCH"
:
@"DEFAULT"
;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
MyselfModel *model = [
self
.datasource[indexPath.section]objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:model.image];
cell.textLabel.text = model.title;
if
([cell.reuseIdentifier isEqualToString:
@"SWITCH"
]) {
UISwitch *aSwitch = [[[UISwitch alloc]init]autorelease];
aSwitch.on =
YES
;
aSwitch.thumbTintColor = [UIColor grayColor];
aSwitch.tintColor = [UIColor orangeColor];
aSwitch.onTintColor = [UIColor orangeColor];
//
单元格的赋值视图
cell.accessoryView = aSwitch;
}
// Configure the cell...
return
cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// FindModel.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<Foundation/Foundation.h>
@interface
FindModel :
NSObject
@property
(
nonatomic
,
retain
)
NSString
*image;
@property
(
nonatomic
,
retain
)
NSString
*title;
@property
(
nonatomic
,
retain
)
NSString
*subtitle;
@end
//
// FindModel.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"FindModel.h"
@implementation
FindModel
- (
void
)dealloc
{
[
_image
release
];
[
_title
release
];
[
_subtitle
release
];
[
super
dealloc
];
}
@end
\
\
//
// MyselfModel.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<Foundation/Foundation.h>
@interface
MyselfModel :
NSObject
@property
(
nonatomic
,
retain
)
NSString
*image;
@property
(
nonatomic
,
retain
)
NSString
*title;
@end
//
// MyselfModel.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"MyselfModel.h"
@implementation
MyselfModel
- (
void
)dealloc
{
[
_image
release
];
[
_title
release
];
[
super
dealloc
];
}
@end
//
// UserModel.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<Foundation/Foundation.h>
@interface
UserModel :
NSObject
@property
(
nonatomic
,
retain
)
NSNumber
*identifier;
@property
(
nonatomic
,
retain
)
NSString
*login;
@property
(
nonatomic
,
retain
)
NSString
*icon;
@end
//
// UserModel.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"UserModel.h"
@implementation
UserModel
- (
void
)dealloc
{
[
_identifier
release
];
[
_login
release
];
[
_icon
release
];
[
super
dealloc
];
}
- (
void
)setValue:(
id
)value forUndefinedKey:(
NSString
*)key{
if
([key
isEqualToString
:
@"id"
]) {
self
.
identifier
= value;
}
}
@end
//
// QiushiModel.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<Foundation/Foundation.h>
#import
<UIKit/UIKit.h>
#import
"UserModel.h"
@interface
QiushiModel :
NSObject
@property
(
nonatomic
,
retain
)
UserModel
*user;
@property
(
nonatomic
,
retain
)
NSNumber
*published_at;
@property
(
nonatomic
,
retain
)
NSString
*content;
@property
(
nonatomic
,
retain
)
NSNumber
*comments_count;
@property
(
nonatomic
,
assign
)
CGFloat
contentHeight;
@end
//
// QiushiModel.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"QiushiModel.h"
@implementation
QiushiModel
- (
void
)dealloc
{
[
_comments_count
release
];
[
_published_at
release
];
[
_user
release
];
[
_content
release
];
[
super
dealloc
];
}
- (
instancetype
)init{
if
(
self
= [
super
init
]) {
self
.
user
= [[[
UserModel
alloc
]
init
]
autorelease
];
}
return
self
;
}
- (
void
)setValue:(
id
)value forUndefinedKey:(NSString *)key{
NSLog(
@"%@"
,key);
}
- (
void
)setValue:(
id
)value forKey:(NSString *)key{
if
([key isEqualToString:
@"user"
]) {
[
self
.user setValuesForKeysWithDictionary:value];
}
else
{
[
super
setValue:value forKey:key];
}
}
- (CGFloat)contentHeight{
CGRect rect = [
self
.content boundingRectWithSize:CGSizeMake(
355
,
1000000
) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:
@{
NSFontAttributeName:[UIFont systemFontOfSize:
15
]
}
context:
nil
];
return
rect.size.height;
}
@end
//
// AppDelegate.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<UIKit/UIKit.h>
@interface
AppDelegate :
UIResponder
<
UIApplicationDelegate
>
@property
(
retain
,
nonatomic
)
UIWindow
*window;
@end
//
// AppDelegate.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"AppDelegate.h"
@interface
AppDelegate
()
@end
@implementation
AppDelegate
- (
void
)dealloc
{
[
_window
release
];
[
super
dealloc
];
}
- (
BOOL
)application:(
UIApplication
*)application didFinishLaunchingWithOptions:(
NSDictionary
*)launchOptions {
// Override point for customization after application launch.
return
YES
;
}
- (
void
)applicationWillResignActive:(
UIApplication
*)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (
void
)applicationDidEnterBackground:(
UIApplication
*)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (
void
)applicationWillEnterForeground:(
UIApplication
*)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (
void
)applicationDidBecomeActive:(
UIApplication
*)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (
void
)applicationWillTerminate:(
UIApplication
*)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// QiushiItemCell.h
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
<UIKit/UIKit.h>
#import
"QiushiModel.h"
@interface
QiushiItemCell :
UITableViewCell
@property
(
retain
,
nonatomic
)
IBOutlet
UIImageView
*userAvatar;
@property
(
retain
,
nonatomic
)
IBOutlet
UILabel
*userName;
@property
(
retain
,
nonatomic
)
IBOutlet
UILabel
*content;
- (
void
)configureCellWithModel:(
QiushiModel
*)model;
@end
//
// QiushiItemCell.m
// QiuShiBaiKeDemo
//
// Created by xalo on 15/9/30.
// Copyright © 2015
年
蓝鸥科技
. All rights reserved.
//
#import
"QiushiItemCell.h"
#import
"UIKit+AFNetworking.h"
@interface
QiushiItemCell
()
@property
(
retain
,
nonatomic
)
IBOutlet
UIView
*bottomView;
@end
@implementation
QiushiItemCell
//
类似初始化的方法
- (
void
)awakeFromNib {
// Initialization code
self
.userAvatar.layer.cornerRadius =
30
;
self
.userAvatar.layer.masksToBounds =
YES
;
}
//
实现自定义方法
#define kAvatarURLFormat @
"http://pic.qiushibaike.com/system/avtnew/%@/%@/medium/%@"
- (
void
)configureCellWithModel:(QiushiModel *)model{
self
.content.text = model.content;
if
(model.user.identifier) {
self
.userName.text = model.user.login;
NSString *picURLString = [NSString stringWithFormat:kAvatarURLFormat,[model.user.identifier.stringValue substringToIndex:model.user.identifier.stringValue.length -
4
],model.user.identifier,model.user.icon];
//
加载对应网址的图片
[
self
.userAvatar setImageWithURL:[NSURL URLWithString:picURLString]];
}
else
{
self
.userName.text =
@"
匿名用户
"
;
self
.userAvatar.image = [UIImage imageNamed:
@"user_icon_anonymous"
];
}
for
(
id
object
in
self
.bottomView.subviews) {
if
([object isKindOfClass:[UILabel class]]) {
UILabel *timeLabel = (UILabel *)object;
NSDate *publishData = [NSDate dateWithTimeIntervalSince1970:model.published_at.doubleValue];
//
将时间戳转成日期
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//
将日期格式化
//
设置日期格式字符串
[formatter setDateFormat:
@"yyyy-MM-dd HH:mm"
];
NSString *timeSting = [formatter stringFromDate:publishData];
[formatter release];
timeLabel.text = timeSting;
}
}
CGRect contentFrame =
self
.content.frame;
contentFrame.size.height = model.contentHeight;
self
.content.frame = contentFrame;
CGRect bottomFrame =
self
.bottomView.frame;
bottomFrame.origin.y = contentFrame.origin.y + model.contentHeight;
self
.bottomView.frame = bottomFrame;
}
- (
void
)setSelected:(
BOOL
)selected animated:(
BOOL
)animated {
[
super
setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (
void
)dealloc {
[_userAvatar release];
[_userName release];
[_content release];
[_bottomView release];
[
super
dealloc];
}
@end