之前写了一篇用控件实现自定义UITableViewCell的,个人觉得还是用代码实现比较好,这样便于维护,而且重用性高。在网上一搜,完全用代码创建UITableView的帖子少之又少,大多数人觉得用IB用方便,但是如果你对完全使用代码创建空间不熟的话,还是建议你用代码,很熟悉的同学可以忽略。 效果图: 先做一些准备工作:先去https://github.com/enormego/EGOTableViewPullRefresh/downloads下载下拉更新的类,这个类只有两个文件,解压后直接拖到项目中来。记得之后在ViewController.h中导入头文件#import "EGORefreshTableHeaderView.h",还需要导入一个库,叫QuartzCore.framework。 还需要下拉箭头的图片,还是去http:///ios/Pull-To-Refresh-TableView/4f681c096803fa2c63000004 下载那个比较完整的项目吧,然后把下拉箭头的图片粘贴过来,大家也可以顺便看看那个项目 下拉框架所需要的准备工作就完成了,接下来是图片异步下载类,下载地址 https://github.com/rs/SDWebImage 这个版本不是最新的,最新的整合成了一个SDWebImage.framework库文件,我尝试了很多次导入成功了,但是运行时会报错,如果有成功运行的同学请告诉我一下哦。 解压之后把SDWebImage文件夹拖到项目中来,里面一共20个文件。 添加两个库:ImageIO.framework和MapKit.framework 如图把Compiler for C/C++/Objective-C 的属性改为Apple LLVM compiler 3.0 最后别忘了导入#import "UIImageView+WebCache.h"头文件 开始写代码! ViewController.h文件 #import <UIKit/UIKit.h> #import "EGORefreshTableHeaderView.h" @interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,EGORefreshTableHeaderDelegate> {//这里别忘了要导入EGORefreshTableHeaderDelegate这个协议,下拉更新必须要用 EGORefreshTableHeaderView *_refreshHeaderView; BOOL _reloading; } @property (retain,nonatomic)NSArray *imageArray; @property (retain,nonatomic)UITableView *tableViewImage; - (void)reloadTableViewDataSource; - (void)doneLoadingTableViewData; @end
ViewController.m文件 ,文中的红字是下拉部分用到的代码,绿色文字是异步更新图片用到的代码#import "ViewController.h" #import "UIImageView+WebCache.h" @implementation ViewController @synthesize imageArray; @synthesize tableViewImage; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; NSArray *array = [NSArray arrayWithObjects: @"http://static2./static/video/451/838/44838154:jpeg_preview_small.jpg?20120509163826", @"http://static2./static/video/656/177/44771656:jpeg_preview_small.jpg?20120509154705", @"http://static2./static/video/629/228/44822926:jpeg_preview_small.jpg?20120509181018", @"http://static2./static/video/116/367/44763611:jpeg_preview_small.jpg?20120509101749", @"http://static2./static/video/340/086/44680043:jpeg_preview_small.jpg?20120509180118", @"http://static2./static/video/666/645/43546666:jpeg_preview_small.jpg?20120412153140", @"http://static2./static/video/771/577/44775177:jpeg_preview_small.jpg?20120509183230", @"http://static2./static/video/810/508/44805018:jpeg_preview_small.jpg?20120508125339", @"http://static2./static/video/152/008/44800251:jpeg_preview_small.jpg?20120508103336", @"http://static2./static/video/694/741/35147496:jpeg_preview_small.jpg?20120508111445", @"http://static2./static/video/988/667/44766889:jpeg_preview_small.jpg?20120508130425", @"http://static2./static/video/282/467/44764282:jpeg_preview_small.jpg?20120507130637", @"http://static2./static/video/754/657/44756457:jpeg_preview_small.jpg?20120507093012", @"http://static2./static/video/831/107/44701138:jpeg_preview_small.jpg?20120506133917", @"http://static2./static/video/411/057/44750114:jpeg_preview_small.jpg?20120507014914", @"http://static2./static/video/894/547/44745498:jpeg_preview_small.jpg?20120509183004", @"http://static2./static/video/082/947/44749280:jpeg_preview_small.jpg?20120507015022", @"http://static2./static/video/833/347/44743338:jpeg_preview_small.jpg?20120509183004", nil]; self.imageArray = array; UITableView *tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain]autorelease]; tableView.dataSource = self; tableView.delegate = self; self.tableViewImage = tableView; [self.view addSubview:tableViewImage]; if (_refreshHeaderView == nil) { EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc]initWithFrame:CGRectMake(0.0f, 0.0f - self.tableViewImage.bounds.size.height, self.view.bounds.size.width, self.tableViewImage.bounds.size.height)]; //注意下拉框的位置描述!要把tableViewImage换成自己定义的tableview! 重点在文中已经注释出来了,应该很明了,如果有任何问题请给我留言。 今天就这么多,想学好的同学们千万不要懒,最差的程序猿就是没完没了地复制粘贴的那些人,复制粘贴之后你真的就会了吗?粘贴完之后能自己写出来吗?还是下次继续复制粘贴?这么做永远都只能保持初级程序员的水平,脑子笨不可怕(我就脑子笨),没有上进心这辈子就完了! |
|
来自: ccccshq > 《UITableView》