




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么写好一个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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 别墅庭院景观设计、施工、绿化养护与景观生态修复协议
- 政府部门云计算信息安全保障框架协议
- 教育机构租赁补充协议
- 2025年网络考试内容分析试题及答案
- 工业气体安全供应与设备检修保障合同
- 国际投资协议法律文本翻译服务合同
- 高级技术文档专业翻译及修订补充协议
- 房产销售虚拟现实技术培训及效果转化协议
- 二级VB阶梯式复习试题及答案
- 法学概论学习交流试题及答案
- 运输供应商年度评价表
- 2023年海南省财金集团有限公司招聘笔试题库及答案解析
- 信息系统项目管理师论文8篇
- 北京中考英语词汇表(1600词汇)
- 超市消防监控系统设计
- 封样管理规定
- 黄腐酸钾项目可行性研究报告-用于立项备案
- 管理人员责任追究制度
- 自动旋转门PLC控制
- 电影场记表(双机位)
- 毕设高密电法探测及数据处理解释
评论
0/150
提交评论