IOS UITableView 使用xib
2014-11-02 21:17
218 查看
使用xib可以灵活的进行UITableViewCell设计。工程结构 xcode6环境我们的UITableView 就是显示一个列表 ,列表每个item实体为Guser
@interface GUser : NSObject@property (strong,nonatomic) NSString *username;@property (strong,nonatomic) NSString *desc;@property (strong,nonatomic) NSString *imgname;+(instancetype)initUserWith:(NSString *)name :(NSString *)desc :(NSString *)icon;@end实现非实例化方法
@implementation GUser+(instancetype)initUserWith:(NSString *)name :(NSString *)desc :(NSString *)icon {GUser *u = [[GUser alloc] init];u.username = name;u.desc = desc;u.imgname = icon;return u;}@end创建MyTableViewCell 封装cell数据合成
@interface MyTableViewCell : UITableViewCell//@property (strong,nonatomic) GUser *user;+(instancetype)initCellWithUser:(GUser *)u;@end实现
+(instancetype)initCellWithUser:(GUser *)u {MyTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"mytable_cell" owner:nil options:nil] lastObject];cell.user = u;cell.username.text = u.username;cell.desc.text = u.desc;cell.icon.image = [UIImage imageNamed:u.imgname];return cell;}创建nib文件ViewControl 代码
#import <UIKit/UIKit.h>#import "GUser.h"#import "MyTableViewCell.h"@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>@property (strong,nonatomic) NSMutableArray *userArray;@end实现
#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UITableView *mytableView;@end@implementation ViewController{}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell01"];GUser *u = self.userArray[indexPath.row];if (cell == nil) {cell = [MyTableViewCell initCellWithUser:u];}NSLog(@"mycell is %@",cell);return cell;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.userArray.count;}- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.UITableView *tableView = (id)[self.view viewWithTag:100];tableView.dataSource = self;tableView.delegate = self;tableView.rowHeight = 80;self.userArray = [[NSMutableArray alloc] initWithCapacity:5];[self.userArray addObject:[GUser initUserWith:@"gaofeng" :@"fix you0" :@"img01.jpg"]];[self.userArray addObject:[GUser initUserWith:@"gaofeng" :@"fix you1" :@"img01.jpg"]];[self.userArray addObject:[GUser initUserWith:@"gaofeng" :@"fix you3" :@"img01.jpg"]];[self.userArray addObject:[GUser initUserWith:@"gaofeng" :@"fix you4" :@"img01.jpg"]];[self.userArray addObject:[GUser initUserWith:@"gaofeng" :@"fix you5" :@"img01.jpg"]];}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@endDone
相关文章推荐
- ios:UITableView下拉刷新的使用
- ios UItableview的使用
- IOS学习:UITableView使用详解3 分组表的简单使用
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
- iOS 6 开发----应用集合视图(UICollectionView)-使用xib文件创建集合视图单元格
- IOS系列——UItableview的基础使用
- ios 中使用 storyboard 来初始化view controller . 从xib文件中获取view
- iOS7中UITableView中cell的使用
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- ios--UITableview的基本使用方法
- iOS—UITableView 的使用(分组形式)
- iOS 之 UITableView 使用索引
- ios--UITableview的基本使用方法
- iOS UITableView的使用大全
- IOS 使用XIB 自定义View
- ios 中使用 Search Display Controller 来对 UITableView 进行前台查询
- IOS开发~UISCrollView与UITableView嵌套使用终极解决方案
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
- iOS 表视图(UITableVIew)的使用方法(1)表视图的示例