cell自定义方式一 高度一致时
来源:互联网 发布:图像识别算法matlab 编辑:程序博客网 时间:2024/06/08 05:42
//——-----------------当cell的高度一致时使用XIB--------------------
步骤:
1.>新建一个xib文件描述一个view的内部结构(假设叫做MJTgCell.xib)
2.>新建一个自定义的类
(自定义类需要继承自系统自带的view,继承自哪个类, 取决于xib根对象的Class)
3.>新建类的类名最好跟xib的文件名保持一致(比如类名就叫做MJTgCell)
4.>将xib中的控件 和 自定义类的.m文件 进行连线
5.>提供一个类方法返回一个创建好的自定义view(屏蔽从xib加载的过程)
6.>提供一个模型属性让外界传递模型数据
7.>重写模型属性的setter方法,在这里将模型数据展示到对应的子控件上面
//——---------------------———————————模型文件M----------------------------------------
继承于NSObject.h#import <Foundation/Foundation.h>@interface MJTg : NSObject/** * 标题 */@property (nonatomic,copy)NSString *title;/** * 价格 */@property (nonatomic,copy)NSString *price;/** * 图片 */@property (nonatomic,copy)NSString *icon;/** * 购买人数 */@property (nonatomic,copy)NSString *buyCount;+ (instancetype)tgWithDict:(NSDictionary *)dict;- (instancetype)initWithDict:(NSDictionary *)dict;@end.m#import "MJTg.h"@implementation MJTg+ (instancetype)tgWithDict:(NSDictionary *)dict{ return [[selfalloc]initWithDict:dict];}- (instancetype)initWithDict:(NSDictionary *)dict{ if (self = [superinit]) { [self setValuesForKeysWithDictionary:dict]; } return self;}@end
//——---------------------———————————视图文件V----------------------------------------
继承于UITableViewCell.h#import <UIKit/UIKit.h>@class MJTg;@interface MJTgCell : UITableViewCell/** * 通过一个tableView来创建一个cell */+ (instancetype)cellWithTableView:(UITableView *)tableView;/** * 团购模型 */@property (nonatomic,strong)MJTg *tg;@end
XIB文件,通过脱线获得属性,红圈处可以设置cell的ID标示
m#import "MJTgCell.h"#import "MJTg.h"@interface MJTgCell()//这些属性均从xib中做了连线@property (weak,nonatomic)IBOutlet UIImageView *iconView;@property (weak,nonatomic)IBOutlet UILabel *titleView;@property (weak,nonatomic)IBOutlet UILabel *priceView;@property (weak,nonatomic)IBOutlet UILabel *buyCountView;@end@implementation MJTgCell+ (instancetype)cellWithTableView:(UITableView *)tableView{ static NSString *ID = @"tg"; MJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { // 从xib中加载cell cell = [[[NSBundle mainBundle] loadNibNamed:@"MJTgCell"owner:niloptions:nil]lastObject]; } return cell;}- (void)setTg:(MJTg *)tg{ _tg = tg; // 1.图片 self.iconView.image = [UIImageimageNamed:tg.icon]; // 2.标题 self.titleView.text = tg.title; // 3.价格 self.priceView.text = [NSStringstringWithFormat:@"¥%@", tg.price]; // 4.购买数 self.buyCountView.text = [NSStringstringWithFormat:@"%@人已购买", tg.buyCount];}@end
//——---------------------———————视图控制器文件C----------------------------------------
继承于UIViewController@property (nonatomic,strong)NSMutableArray *tgs;/** * 数据的懒加载 */- (NSMutableArray *)tgs{ if (_tgs ==nil) { // 初始化 // 1.获得plist的全路径 NSString *path = [[NSBundlemainBundle]pathForResource:@"tgs.plist"ofType:nil]; // 2.加载数组 NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 NSMutableArray *tgArray = [NSMutableArrayarray]; for (NSDictionary *dictin dictArray) { // 3.1.创建模型对象 MJTg *tg = [MJTgtgWithDict:dict]; // 3.2.添加模型对象到数组中 [tgArray addObject:tg]; } // 4.赋值 _tgs = tgArray; } return _tgs;}#pragma mark - 数据源方法/** * 一共有多少行数据 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.tgs.count;}/** * 每一行显示怎样的cell */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 1.创建cell MJTgCell *cell = [MJTgCellcellWithTableView:tableView]; // 2.给cell传递模型数据 cell.tg = self.tgs[indexPath.row]; return cell;}
0 0
- cell自定义方式一 高度一致时
- cell自定义方式二 高度不一致时
- 自定义cell的高度
- UI_UItableView_AutoCell(自定义cell 高度)
- 自定义cell的高度
- xib 自定义cell高度
- 团购(cell高度相同时自定义cell)
- xib自定义cell,返回cell高度
- iOS 自定义Cell 自适应高度
- iOS 自定义cell的高度
- UI 10 cell 自定义高度
- UI - TableView高级(自定义cell、cell自定义高度)
- 当cell的高度不一致时,如何通过代码自定义cell
- 一. 使用代码实现UITableView的cell自定义高度-类似于微博主页
- UITableView-的cell 自定义 高度,样式
- 根据文字自定义Label和Cell高度
- 自定义cell的高度(新浪微博)
- 自定义cell高度实现简单通讯录
- Linux学习与总结-1入门概念
- 竞赛题: 回文问题
- 事件冒泡机制
- UIcolor RGB颜色
- Web前端学习——JS基础二之图片切换
- cell自定义方式一 高度一致时
- Android控件详解之ImageView
- 游标操作
- QBC运算符含义
- samba应用常见问题 和 修改后的smb.conf文件
- centos7下Mairadb忘记密码修改
- Android基础入门教程——1.3 SDK更新不了问题解决
- C#基础加强第一天总结--面向对象1
- Android基础入门教程——1.4 Genymotion模拟器安装