使用 xib 方式自定义 cell
tableV iew 使用代码方式自定义 cell
coll ectionView 使用代码方式自定义 cell
原型 cell

静态cell

    使用xib方式自定义cell:
    1.创建一个自定义的cell类,并继承UITableViewCell
    2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
    3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
    4.拖线生成属性
    5.cell定义一个模型属性,并重写该模型属性的set方法
    计算机生成了可选文字:+ (instancetype)goodsCellWithTableView: (UI TableView *)tableView { [tableView dequeueReusableCellWithIdentifier:ID] ; HMGoodsCell *cell 1 ce = [ [ [NSBundle mainBundle] owner: nil options:nil] lastObject] ; cell return cell;

    原型cell的方式自定义cell:
    1.创建一个自定义的cell类,并继承UITableViewCell
    2.设置xib文件里面的cell需要和自定义的cell相关联(在类型控制器中更改class)
    3.设置xib文件中属性控制器下的可重用ID,要和自定义cell中的相同
    4.拖线生成属性
    5.cell定义一个模型属性,并重写该模型属性的set方法
    计算机生成了可选文字:(UITableViewCeII *)tableView: (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath * ) indexPath { / / 1. 获 取 模 型 数 据 self apps[indexPath. row HMApp *app / / 2 . 创 建 Cell static NSString *ID = @"app_cell" [tableView dequeueReusableCellWithIdentifier:ID]; HMAppCeII *cell / / 为 每 个 Ce 且 指 定 一 个 代 理 cell.delegate self, / / 3 · 把 模 型 数 据 设 置 给 cell cell. app app; / / 4 · 返 回 Cell return cell; 这 里 只 需 要 在 缓 存 池 中 查 找 相 同 旧 的 ce 丨 丨 就 OK 了 , 如 果 缓 存 池 中 没 有 , 系 统 就 根 据 原 型 c 酬 重 新 创 建 一 个 , 不 需 要 我 们 手 动 创

    原型cell和xib创建cell的不同点:
    xib创建cell比原型cell多一步(在缓存池中没有找到cell,xib方式需要自己手动创建一个cell,而原型cell不需要手动创建,系统会自己根据原型cell的模板来创建)

    tableView代码方式自定义cell:
    1.创建一个自定义的cell类,并继承UITableViewCell
    2.手动在类扩展中定义需要的控件属性
    3.重写initWithStyle方法,在方法里面创建子控件,并赋值给类扩展中的属性(这里就相当于拖线)
    4.cell定义一个模型属性,并重写该模型属性的set方法
    计算机生成了可选文字:#pragma mark - + (instancetype)weiboCellWithTableView: (UI TableView *)tableView { static NSString *ID — @"weibo_cell"; - [tableView dequeueReusableCellWithIdentifier. HMWeiboCell *cell - (cell =- nil) { if [ [HMWeiboCell al loc] reuseldentifier: ID] ; cell - cell; return

    tableView的团购案例:
    xib方式创建单元格
    计算机生成了可选文字:> 14 Placeholders File's Owner First Responder goods_cell Views Image View x HMGoodsCell.xib goods_cell Label Label e View Cell Style Identifier Selection Accessory Editing Acc. Indentation Label Custom goods_cell Default None None Level 10 Width O Indent While Editing Shows Re-order Controls Separator Default Insets

    计算机生成了可选文字:#import <UIKit/UIKit. @class HMGoods; @interface HMGoodsCell . UlTab1eViewCe11 UlTableViewCell @property (nonatomic, strong) HMGoods *goods ; + (instancetype)goodsCellWithTableView: (UI TableView *)tableView; @end

    计算机生成了可选文字:#pragma mark - + (instancetype)goodsCellWithTableView: (UI TableView *)tableView { static NSString *ID = *cell = [tableView dequeueReusableCellWithIdentifier:ID] ; HMGoodsCell if (cell nil) { = [ [ [NSBundle mainBundle] owner: nil cell options: nil] lastObject]; return cell;

    xib方式创建footerView(headerView和footerView方式一样):
    计算机生成了可选文字:Views HMGoodsFooterView.xib Goods Footer View Placeholders File's Owner First Responder Goods Footer View Custom Class Module Identity Restoration ID HMGoodsFooterView None User Defined Runtime Attributes Key Path Type *RID Document Value Title Item

    计算机生成了可选文字:#import <UIKit/UIKit. @class HMGoodsFooterView; @protocol HMGoodsFooterViewDelegate <NS0bject> - (void)goodsFooterViewDidClickLoadMoreButton: (HMGoodsFooterView *)footerView; @end UlView @interface HMGoodsFooterView : UIView @property (nonatomic, weak) id<HMGoodsFooterViewDelegate > delegate; + (instancetype)goodsFooterView; @end

    计算机生成了可选文字:+ (instancetype)goodsFooterView { *footerView = [ [ [NSBundle mainBundle] HMGoodsFooterView loadNibNamed:@"HMGoodsFooterView'I owner: nil options: nil] lastObject] ; return footerView•

    计算机生成了可选文字:#pragma mark - viewDidLoad - (void)viewDidLoad { [super viewDidLoad] ; self. tableView. rowHeight // jaäEfif00terView HMGoodsFoote rV iew *foote rV iew self. tableView. tableFooterView = footerView; // view HMGoodsHeade rV iew *heade rV iew self. tableView. tableHeaderView = headerView; iMiü#fifiit EheaderView tableView* [HMGoodsFooterView goodsFooterView] ; [HMGoodsHeaderView goodsHeaderView] ;

    tableView的微博案例(QQ聊天布局案例和它相同):
    代码方式创建单元格:
    计算机生成了可选文字:#import <UIKit/UIKit. @class HMWeiboFrame; @interface HMWeiboCell . UlTab1eViewCe11 UlTaDleViewCell @property (nonatomic, strong) HMWeiboFrame *weiboFrame; + (instancetype)weiboCellWithTableView: (UI TableView *)tableView; @end

    计算机生成了可选文字:#pragma mark - + (instancetype)weiboCellWithTableView: (UI TableView *)tableView { static NSString *ID = *cell = [tableView dequeueReusableCellWithIdentifier:ID] ; HMWeiboCell if (cell nil) { [ [HMWeiboCell al loc] initWithStyle: ITableViewCellStyleDefault cell reuseldentifier: ID] ; return cell; #pragma mark - iMjä initWithStyIe - (instancetype) initWithStyle: (UI TableViewCellStyle)style reuseldentifier: (NSString reuseldentifier { 1 se super Inl 1 y e:s y e reuse UllmageView *imgViewIcon = [ [UllmageView en 1 ler: reuse en 1 ler al loc] init] ;

    tableView的QQ好友列表案例:
    代码方式创建组头、组尾:
    计算机生成了可选文字:#import <UIKit/UIKit. @class HMGroup; @interface HMGroupHeaderView : UI TableViewHeaderFooterView @property (nonatomic, strong) HMGroup *g roup ; + (instancetype)groupHeaderViewWithTableView: (UI Tabl @end

    计算机生成了可选文字:+ (instancetype)groupHeaderViewWithTableView: (UI TableView *)tableView { static NSString *ID = @"group_header_view"; *headerVw = [tableView HMG roupHeade rV iew dequeueReusableHeaderFooterViewWithIden inltWithReuseI if (headerVw nil) { // initWithReuseIdentifier headerVw = [[HMGroupHeaderView alloc] initWithReuseIdentifier: D] ; return headerVw; // initWithReuseIdentifier - (instancetype) initWithReuseIdentifier: (NSString reuseldentifier { [super initWithReuseIdentifier: reuseldentifier]) { if (self - // header

    计算机生成了可选文字:// äEXGÆfiheader - (UIView *)tableView: (UI TableView *)tableVie viewForHeaderInSection: section { i*tabIeView = self.groups[section] ; HMGroup roup // 2. view NSInteger) *headerVw = [HMGroupHeaderView groupHeaderViewWithTableView: HMG roupHeade rV iew tableView] ; // header view // fEZ: GheaderVw, Gtable view // ißLGtable view2äjåN}Etable viewä9fiiQN, viewfif rame // FfiL\Å, view —FEfiframeÆO, header viewfio heade rVw. group = group; // 4. jfi@header view return headerVw;

    tableView的应用管理案例:
    原型cell方式创建单元格:
    计算机生成了可选文字:Views M...rd Table View Controller Scene Table View Controller v app_cell app_cell < Table View ontent View First Respon Exit Storyboard Entry Point Prototype Cells Label Image Label w Cell Style Custom Identifier app_cell Accessory None Editing Acc. None Indentation Level Width O Indent While Editing 000 10 Table View Prototype Content

    计算机生成了可选文字:#import <UIKit/UIKit.h> @class HMApp, HMAppCell; @protocol HMAppCellDelegate 4SObject> - (void)appCellDidClickDownloadButton: (HMAppCell appCell; @end @interface HMAppCe11 . @property (nonatomic, @property (nonatomic, @end UlTableViewCell UlTab1eViewCe11 weak) id<HMAppCellDelegate > delegate; strong) HMApp *app;

    计算机生成了可选文字:- (UI TableViewCell *)tableView: (UI TableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *ID = = [tableView dequeueReusableCellWithIdentifier:ID] ; HMAppCe11 *cell cell. delegate = self; cell. app - app; // 4. jÉ@Cell return cell;

    collectionView的案例:
    纯代码自定义cell:
    计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMAppCollectionViewCell . UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end

    计算机生成了可选文字:// initWithFrame - (instancetype) initWithFrame: (CGRect)frame { [super initWithFrame:frame]) { if (self - = [ [UllmageView al loc] UllmageView *imgViewIcon [self. contentView addSubview: imgViewIcon] ; self . imgViewIcon imgV iewlcon ; // label = [[UILabel al loc] init] ; UILabel *lblName lblName. font = [UIFont systemFont0fSize: 12] ; lblName. textAlignment = NSTextAlignmentCenter; [self. contentView addSubview: lblName] ; self . lblName = lblName; // button = [[UIButton al loc] init] ; UIButton *button init] ; [button setBackgroundImage: [UI Image imageNamed:@Ibuttongreen"] forState: UICont rolStateNormal] ;

    下面这行代码会执行initWithFrame方法
    HMAppCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    计算机生成了可选文字:static NSString * const ID - void viewDidLoad [super viewDidLoad] ; [self. collectionView registerClass: [HMAppCollectionViewCell class forCellWithReuseIdent1f1er // iQNCollection = [UIColor whiteColor] ; self. collectionView. backg roundColor - (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath { = self. apps [indexPath. item] ; *model HMApp cell HMAppCol lectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jä@cell return cell;

    xib方式自定义cell:
    计算机生成了可选文字:> 14 Placeholders File's Owner First Responder app_cell Img Vie on Lbl Name @Btn Download Constraints Views HMAppCollectionViewCell.xib äEXficeII Ima Label app_cell eusable View Identifier app_cell View Mode Center Semantic Unspecified Tag Interaction O User Interaction Enabled O Multiple Touch Alpha Background Default Tint Default 1 Drawing Opaque Hidden O Clears Graphics Context O Clip Subviews O Autoresize Subviews Stretching x 1 1

    计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMAppCollectionViewCell . UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end

    计算机生成了可选文字:static NSString * const ID - (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath {l = self. apps [indexPath. item] ; *model HMApp // 2. @lJECe11 HMAppCol lect ionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jä@cell return cell; #pragma mark - viewDidLoad - (void)viewDidLoad { [super viewDidLoad] ; iMiä registerNib UINib *nib = [UINib nibWithNibName:@"HMAppCollectionViewCell" bundle: nil] ; [self. collectionView registerNib: nib forCellWithReuseIdentifier: = [UIColor whiteColor] ; self. collectionView. backg roundColor

    原型cell方式自定义cell:
    计算机生成了可选文字:> 14 ä EXCell Collection View app_cell O View Controller Scene Collection View app_cell Colle Vie... First Respon er Exit Storyboard Entry Poi... Collection View Contr... View Controller Scene > coil View Ima Label Identifier app_cell Mode Center Semantic Unspecified Tag Interaction O User Interaction Enabled O Multiple Touch Alpha Background Default Tint Default 1 Drawing Opaque Hidden O Clears Graphics Context O Clip Subviews O Autoresize Subviews Stretching x 1 1

    计算机生成了可选文字:#import <UIKit/UIKit. @class HMApp; @interface HMCollectionViewCell UICollectionViewCell @property (nonatomic, strong) HMApp *app; @end

    计算机生成了可选文字:- (UICollectionViewCell *)collectionView: (UICollectionView *)collectionView cellForItemAtIndexPath: (NSIndexPath indexPath { = self. apps [indexPath. item] ; *model HMApp // (prototype cell) static NSString *ID = = [collectionView HMCollectionV iewCell *cell dequeueReusableCellWithReuseIdentifier:ID forlndexPath: indexPath] ; cell. = model; app // 4. jÉ@Cell return cell;

    代码方式创建组头、组尾:
    计算机生成了可选文字:#import <UIKit/UIKit. @class HMPhoto; // UICollectionReusa @interface HMSectionHeaderView : UICollectionReusableView @property (nonatomic, strong) HMPhoto *photo ; @end

    计算机生成了可选文字:- (instancetype) initWithFrame: (CGRect)frame { [super initWithFrame:frame]) { if (self = [[UILabel al loc] init] ; UILabel *lblTitle lblTitle. font = [UIFont systemFontOfSize: 12] ; lblTitle. textAlignment = NSTextAlignmentCenter; [self addSubview: lblTitle] ; self . lblTitle = lblTitle; self. backg roundColor return self; = [UIColor

    计算机生成了可选文字:- (void)viewDidLoad { [super viewDidLoad] ; [self. collectionView registerClass: [HMPhotoCollectionViewCell class] forCellWithReuseIdentifier: CGFloat itemW = self. collectionView. bounds. size. width; CGFloat itemH = itemW; = CGSizeMake( itemW, itemH) ; self. flowLayout . itemSize // view [self. collectionView registerClass: [HMSectionHeaderView class] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier:@"header"] ; // Efooter View [self. collectionView registerClass: [HMSectionFooterView class] forSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"] ; // i±MGÆfiheader footerfiX/J\ self. flowLayout . heade rRefe renceSize self. flowLayout. footerReferenceSize = CGSizeMake(0, = CGSizeMake(0, 49) ; 49) ;
    计算机生成了可选文字:// footerfi-k/J\, - (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElement0fKind: (NSString *)kind atlndexPath: (NSIndexPath *)indexPath { *model = self. photos [indexPath.section] ; HMPhoto static NSString *ID; 1. header footer if ( [kind isEqualToString:UICollectionElementKindSectionHeader]) { // ID = @lheader"; else { ID = @lfooter"; IjtfÄ 2. — I header 0 footer [collectionView de ueueReusableSu UICollectionReusableView *headerFooterView = withReuseIdentifier:ID forlndexPath: indexPath] ; if ( [kind isEqualToString:UICollectionElementKindSectionHeader]) { // lementar View0fKind: ind HMSectionHeaderView *heade rVw heade rVw. photo = model; } else { HMSectionFooterV iew *foote rVw foote rVw. photo = model; return headerFooterView; headerFooterView; HMSect ionHeade rV iew headerFooterView; HMSectionFooterView

    tableViewCell、collectionViewCell、组头组尾等总结相关推荐

    1. iOS之UITableView组头组尾视图/标题悬停

      最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...

    2. iOS 设置tableview组头组尾的背景色

      直接设置UITableViewHeaderFooterView的backgroundView.backgroundColor或者的backgroundColor,都是没有用的,马克一下设置的table ...

    3. UICollectionView 自定义组头组尾的XIB方法

      UICollectionView的加载方式和Tableview很像,基本上加载的方法都差不多,尤其是它的数据源的方法和代理方法基本上类似,只不过是名字上有点细微的差别而已.这里面不赘述. 1. UIC ...

    4. UICollectioView的组头和组尾的高度不同设置

      有些需要组头和组尾的高度不同,在layout中设置导置所有的组头和组尾的高都一样,刚做了个项目正好有这个需要,代码如下 - (CGSize)collectionView:(UICollectionVi ...

    5. iphone开发之表格组件UITableView的使用(二)如何分组展示数据并添加组头和组尾描述

      1.在实现UITAbleView数据源协议即UITableViewDataSource内的如下方法中: //  此方法用于告诉哪个UITableView的每一组的每一行显示什么内容 -(UITable ...

    6. iOS UITableView 指定组头悬停位置

      悬停位置 当UITableView占满屏幕时 组头默认是在顶部悬停 在机型 iPhone X 上 会被顶部的刘海给挡住 这效果显然不能直视~ 理想的效果是: 在导航下方悬停 (导航是个自定义的视图 带 ...

    7. 【iOS 1 行代码系列】之 一行代码搞定TableView组头悬停

      场景: 当 UITableView 的 style 属性设置为 Plain 时 tableview 的 section header 在滚动到界面顶端时 会 悬停 ! 疑问: 1.如何在不使用Grou ...

    8. Xamarin 设置ListView组头的高度

      1.自定义组头Cell,重写绑定内容改变事件,在该事件中设置组头的高度. public class GroupCe11 : ViewCe11{protected override void OnBin ...

    9. 强大的分组SectionListView, 支持自定义组头布局和分组的内容布局, 组头有挤压效果

      效果与系统的联系人分组效果类似,组头有挤压效果.但是不同的是,SectionListView支持每组的组头布局自定义,每组的内容布局也可以自定义. 效果图如下: 自定义Adapter: package ...

    最新文章

    1. 如何学习大数据!!我要做大数据!
    2. ajax返回显示下拉列表,ajax中网页传输(二)JSON——下拉列表显示练习(示例代码)...
    3. [html] 如何在网页中嵌入公司地址的地图?
    4. RAISERROR (Transact-SQL)
    5. java comparator_Java 集合排序策略接口 Comparator
    6. HTML Web教程
    7. 差点被威金病毒搞死了……
    8. 【壁纸小程序】搭建自己的壁纸小程序-微信抖音双端
    9. vscode中文注释斜体修改
    10. 【vue】To install them, you can run: npm install --save aws-sdk module
    11. easyui datagrid合并列中相同的单元格
    12. Cad二次开发小工具
    13. 查看、管理和修饰数码照片的利器:Windows照片库
    14. 学习 PixiJS — 小精灵冒险
    15. 创建 Pynq Overlay
    16. Re01:NerLTR-DTA: drug–target binding affinity prediction based on neighbor relationship and learning
    17. 位图文件(BMP)格式分析以及程序实现
    18. android手机号码恢复,安卓手机通讯录怎么恢复?教你恢复小妙招
    19. 常用的企业管理软件有哪些?
    20. wordpress谷歌字体_如何以“正确”的方式在WordPress主题中添加Google Web字体

    热门文章

    1. GPT-3会是第一个通用人工智能吗?
    2. opencv位运算,cv2.bitwise_and,cv2.bitwise_or,cv2.bitwise_not,cv2.bitwise_xor
    3. 过程分析对进行有针对性ICS攻击的意义
    4. 1万小时缔造的世界第一程序员
    5. java 去掉pdf文字_Java 删除PDF中的附件
    6. js获取传统节假日_js 两个时间之间工作日的计算问题(包含节假日)
    7. 这个时代,达不到百万级以上的并发量,都不叫高并发!!!
    8. 吐槽弹幕网,解决映兔源无法播放的书签插件
    9. 【心电信号】基于matlab心率检测【含Matlab源码 1993期】
    10. 关于js导入Excel时,Excel的(年/月/日)日期是五位数字的问题。以及对Excel日期存在的错误的分析和处理。