版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么写好一个UITableView
数据源-
(NSInteger)tableView:(UITableView
*)tableView
numberOfRowsInSection:(NSInteger)section;
-
(UITableViewCell
*)tableView:(UITableView
*)tableView
cellForRowAtIndexPath:(NSIndexPath
*)indexPath;代理-
(CGFloat)tableView:(UITableView
*)tableView
heightForRowAtIndexPath:(NSIndexPath
*)indexPath;
-
(void)tableView:(UITableView
*)tableView
didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;SectionObject@interface
KtTableViewSectionObject
:
NSObject
@property
(nonatomic,
copy)
NSString
*headerTitle;
//
UITableDataSource
协议中的
titleForHeaderInSection
方法可能会用到
@property
(nonatomic,
copy)
NSString
*footerTitle;
//
UITableDataSource
协议中的
titleForFooterInSection
方法可能会用到
@property
(nonatomic,
retain)
NSMutableArray
*items;
-
(instancetype)initWithItemArray:(NSMutableArray
*)items;
@endItem@interface
KtTableViewBaseItem
:
NSObject
@property
(nonatomic,
retain)
NSString
*itemIdentifier;
@property
(nonatomic,
retain)
UIImage
*itemImage;
@property
(nonatomic,
retain)
NSString
*itemTitle;
@property
(nonatomic,
retain)
NSString
*itemSubtitle;
@property
(nonatomic,
retain)
UIImage
*itemAccessoryImage;
-
(instancetype)initWithImage:(UIImage
*)image
Title:(NSString
*)title
SubTitle:(NSString
*)subTitle
AccessoryImage:(UIImage
*)accessoryImage;
@end父类实现代码-
(NSInteger)tableView:(UITableView
*)tableView
numberOfRowsInSection:(NSInteger)section
{
if
(self.sections.count
>
section)
{
KtTableViewSectionObject
*sectionObject
=
[self.sections
objectAtIndex:section];
return
sectionObject.items.count;
}
return
0;
}优势+
(CGFloat)tableView:(UITableView*)tableView
rowHeightForObject:(KtTableViewBaseItem
*)object;@protocol
KtTableViewDelegate<UITableViewDelegate>
@optional
-
(void)didSelectObject:(id)object
atIndexPath:(NSIndexPath*)indexPath;
-
(UIView
*)headerViewForSectionObject:(KtTableViewSectionObject
*)sectionObject
atSection:(NSInteger)section;
//
将来可以有
cell
的编辑,交换,左滑等回调
//
这个协议继承了UITableViewDelegate
,所以自己做一层中转,VC
依然需要实现某
@end
@interface
KtBaseTableView
:
UITableView<UITableViewDelegate>
@property
(nonatomic,
assign)
id<KtTableViewDataSource>
ktDataSource;
@property
(nonatomic,
assign)
id<KtTableViewDelegate>
ktDelegate;
@end-
(CGFloat)tableView:(UITableView*)tableView
heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
id<KtTableViewDataSource>
dataSource
=
(id<KtTableViewDataSource>)tableView.dataSource;
KtTableViewBaseItem
*object
=
[dataSource
tableView:tableView
objectForRowAtIndexPath:indexPath];
Class
cls
=
[dataSource
tableView:tableView
cellClassForObject:object];
return
[cls
tableView:tableView
rowHeightForObject:object];
}优势@interface
KtTableViewController
:
UIViewController<KtTableViewDelegate,
KtTableViewControllerDelegate>
@property
(nonatomic,
strong)
KtBaseTableView
*tableView;
@property
(nonatomic,
strong)
KtTableViewDataSource
*dataSource;
@property
(nonatomic,
assign)
UITableViewStyle
tableViewStyle;
//
用来创建
tableView
-
(instancetype)initWithStyle:(UITableViewStyle)style;
@end所有的计算机问题,都可以通过添加中间层来解决三大模块+
(void)networkTransferWithURLString:(NSString
*)urlString
andParameters:(NSDictionary
*)parameters
isPOST:(BOOL)isPost
transferType:(NETWORK_TRANSFER_TYPE)transferType
andSuccessHandler:(void
(^)(id
responseObject))successHandler
andFailureHandler:(void
(^)(NSError
*error))failureHandler
{
//
封装AFN
}@interface
DRDBaseAPI
:
NSObject
@property
(nonatomic,
copy,
nullable)
NSString
*baseUrl;
@property
(nonatomic,
copy,
nullable)
void
(^apiCompletionHandler)(_Nonnull
id
responseObject,
NSError
*
_Nullable
error);
-
(void)start;
-
(void)cancel;
...
@end如何回调[数据解析](/how-to-create-an-uitableview/#)//
KtBaseItem.m
-
(void)parseData:(NSDictionary
*)data
{
//
解析
data
这个字典,为自己的属性赋值
//
具体的实现请见后面的文章
}封装API对象Model与ItemBaseModel@interface
KtBaseModel
//
请求回调
@property
(nonatomic,
copy)
KtModelBlock
completionBlock;
//网络请求
@property
(nonatomic,retain)
KtBaseServerAPI
*serverApi;
//网络请求参数
@property
(nonatomic,retain)
NSDictionary
*params;
//请求地址
需要在子类init中初始化
@property
(nonatomic,copy)
NSString
*address;
//model缓存
@property
(retain,nonatomic)
KtCache
*ktCache;BaseItem-
(void)parseData:(NSDictionary
*)data
{
Class
cls
=
[self
class];
while
(cls
!=
[KtBaseItem
class])
{
NSDictionary
*propertyList
=
[[KtClassHelper
sharedInstance]
propertyList:cls];
for
(NSString
*key
in
[propertyList
allKeys])
{
NSString
*typeString
=
[propertyList
objectForKey:key];
NSString*
path
=
[self.jsonDataMap
objectForKey:key];
id
value
=
[data
objectAtPath:path];
[self
setfieldName:key
fieldClassName:typeString
value:value];
}
cls
=
class_getSuperclass(cls);
}
}如何使用ListItem//
In
.h
@interface
KtBaseListItem
:
KtBaseItem
@property
(nonatomic,
assign)
int
pageNumber;
@end
//
In
.m
-
(id)initWithData:(NSDictionary
*)data
{
if
(self
=
[super
initWithData:data])
{
self.pageNumber
=
[[NSString
stringWithFormat:@"%@",
[data
objectForKey:@"page_number"]]
intValue];
}
return
self;
}ListModel@protocol
KtBaseListModelProtocol
<NSObject>
@required
-
(void)refreshRequestDidSuccess;
-
(void)loadRequestDidSuccess;
-
(void)didLoadLastPage;
-
(void)handleAfterRequestFinish;
//
请求结束后的操作,刷新tableview或关闭动画等。
@optional
-
(void)didLoadFirstPage;
@end
@interface
KtBaseListModel
:
KtBaseModel
@property
(nonatomic,
strong)
KtBaseListItem
*listItem;
@property
(nonatomic,
weak)
id<KtBaseListModelProtocol>
delegate;
@property
(nonatomic,
assign)
BOOL
isRefresh;
//
如果为是,表示刷新,否则为加载。
-
(void)loadPage:(int)pageNumber;
-
(void)loadNextPage;
-
(void)loadPreviousPage;
@endRefreshTableViewController#pragma
-mark
KtBaseListModelProtocol
-
(void)loadRequestDidSuccess
{
[self
requestDidSuccess];
}
-
(void)refreshRequestDidSuccess
{
[self.dataSource
clearAllItems];
[self
requestDidSuccess];
}
-
(void)handleAfterRequestFinish
{
[self.tableView
stopRefreshingAnimation];
[self.tableView
reloadData];
}
-
(void)didLoadLastPage
{
[self.tableView.mj_footer
endRefreshingWithNoMoreData];
}
#pragma
-mark
KtTableViewDelegate
-
(void)pullUpToRefreshAction
{
[self.listModel
loadNextPage];
}
-
(void)pullDownToRefreshAction
{
[self.listModel
refresh];
}实际使用-
(void)viewDidLoad
{
[super
viewDidLoad];
[self
createModel];
//
Do
any
additional
set
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 风能设备采购合同
- 2023年床上用织物制品资金筹措计划书
- 模板支设施工合同7篇
- 安徽省六安市霍邱县马店中学2022年高一数学文月考试题含解析
- 2022年江西省宜春市山林岗中学高一数学文期末试题含解析
- 广东省汕头市正始中学高一数学文下学期摸底试题含解析
- 山东省泰安市崔村中学高一英语月考试题含解析
- 2022年贵州省遵义市仁怀城南中学高一数学文测试题含解析
- 山东省青岛市平度开发区高级中学高一数学文模拟试题含解析
- 天津宝坻区育英中学 高一数学文期末试题含解析
- 高中地理学业水平考试复习课件 专题八 水循环
- WS/T 510-2016病区医院感染管理规范
- GB/T 2900.50-2008电工术语发电、输电及配电通用术语
- 第六章设计心理的宏观分析
- 硝酸腐蚀速率汇编
- 《野蜂飞舞》阅读测试题及答案
- 2022年中电金信数字科技集团有限公司招聘笔试试题及答案解析
- 高二化学期中考试质量分析
- 粘土湿型砂及其质量控制课件
- 区域检验中心项目构建书-定稿
- 实验室内温湿度记录表
评论
0/150
提交评论