做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头。

在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上会有一定的不同

tableView

1.纯代码自定义cell,直接用init方法自定义,然后在UITableViewCell* 里面自己根据标识加载

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];

if (self) {

[self initSubview];

}

return  self;

}

-(void)initSubview{

_monthLabel = [[UILabel alloc]init];

[self.contentView addSubview:_monthLabel];

_moneyLabel=[[UILabel alloc]init];

[self.contentView addSubview:_moneyLabel];

_sepLineView = [[UIView alloc]init];

[self.contentView addSubview:_sepLineView];

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString*cellID = @"ProperTableViewCell";

ProperTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell==nil) {

cell = [[ProperTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

}

2.用了xib拖线自定义cell

xib拖线自定义需要注意点:在vc中加载时需要注册

[_tableView registerNib:[UINib nibWithNibName:[ ProperTableViewCell class] bundle:nil] forCellReuseIdentifier:@"ProperTableViewCell"];

或者

//注册headerView Nib的view需要继承UICollectionReusableView

[self.collectionView registerNib:[UINib nibWithNibName:@"ProductItemCell" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductItemCell"];

然后根据标识找到对应的cell

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString*cellID = @"ProperTableViewCell";

ProperTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell==nil) {

cell = [[ProperTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

}

3.如果cell是用xib自定义的,在cell的加载那里注册之后,可以用nib的方法来加载

static NSString * CellIdentifier = @"ProductItemCell";

if(!nibri)

{

UINib *nib = [UINib nibWithNibName:@"ProductItemCell" bundle:nil];

[self.collectionView registerNib:nib forCellWithReuseIdentifier:CellIdentifier];

nibri =YES;

}

ProductItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

4.注意collectionView和tableView的混合使用

collectionView和tableview 的混合使用时,需要数据实时刷新,如果重用数据的话需要用nib方式加载,不然会出现数据错乱情况

用下面这种方法来创建cell

// 重用数据会用的是缓存池的数据没有刷新,但是tableviewcell每次滚动时数据会重用,所以不能用标识重用

NSArray *nib =[[NSBundle mainBundle]loadNibNamed:@"hangyeCell" owner:nil options:nil];

hangyeCell *cell = [nib objectAtIndex:0];

ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用的更多相关文章

  1. WinForm中自定义搜索框(水印、清空按钮、加载中图标)

    public partial class CustomSearchBar : TextBox { private readonly Label lblwaterText = new Label(); ...

  2. iOS开发 XML解析和下拉刷新,上拉加载更多

    iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...

  3. 关于ligerui 中 grid 表格的扩展搜索功能在远程数据加载时无法使用的解决办法

    要想使用grid里的扩展搜索功能,除了要引用ligerui主要的js文件外,还必须引入下面的JS文件: 1.Source\demos\filter\ligerGrid.showFilter.js 2. ...

  4. win7/8系统中php5.3和5.4、5.5不能加载php_curl.dll解决办法

    win7/8系统中php5.3和5.4.5.5不能加载php_curl.dll解决办法   作者:用户 来源:互联网 时间:2016-06-23 18:54:33 php变量注释系统模块 摘要: 本文 ...

  5. 在NVIDIA A100 GPU中使用DALI和新的硬件JPEG解码器快速加载数据

    在NVIDIA A100 GPU中使用DALI和新的硬件JPEG解码器快速加载数据 如今,最流行的拍照设备智能手机可以捕获高达4K UHD的图像(3840×2160图像),原始数据超过25 MB.即使 ...

  6. ios中自定义cell 设置cell的分组结构

    ios系统默认的cell并不能满足我们的需求 这个时候就需要自定义我们的cell 自定义cell为分组的时候 需要设置分组样式  以下是我常用分组的二种方法: 第一是 在自定义的UITableView ...

  7. iOS中 自定义cell分割线/分割线偏移 韩俊强的博客

    在项目开发中我们会常常遇到tableView 的cell分割线显示不全,左边会空出一截像素,更有甚者想改变系统的分割线,并且只要上下分割线的一个等等需求,今天重点解决以上需求,仅供参考: 每日更新关注 ...

  8. iOS中自定义UITableViewCell的用法

    1.先创建一个View继承 UITableViewCell并使用xib快速建立模型. #import <UIKit/UIKit.h> #import "Score.h" ...

  9. Nib加载的方式实现自定义TableView

    实现的效果 实现原理: 通过在主界面中用Bundle 的LoadNib的方式进行自定义窗体文件加载并渲染界面 实现步骤 Step One 创建TableViewCell 窗体 Step Two 定义接 ...

随机推荐

  1. webstrom配置sass与less

    1.less 安装一个稳定版的node.[例如node-v4.4.4-x64] 然后直接在webstrom里导入那个lessc.cmd 2.sass 安装ruby. 安装完之后点开,Start那个安装 ...

  2. soundtouch变速wsola算法之改进

    soundtouch变速算法很类似sola算法,细看才知道是wsola算法. 上个星期有个需求,将该变速应用到直播的包处理,有点类似于webrtc的netEQ处理机制. 直接使用soundtouch, ...

  3. GET和POST

    Ajax与Comet 1. Ajax Asynchronous Javascript+xml :能够向服务器请求额外的数据而无需卸载页面. Ajax技术的核心是XMLHttpRequest 对象(简称 ...

  4. LeetCode 88 Merge Sorted Array

    Problem: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  5. SQL Server 临时禁用和启用所有外键约束(高版本向低版本迁移数据)

    --获得禁用所有外键约束的语句 select 'ALTER TABLE [' + b.name + '] NOCHECK CONSTRAINT ' + a.name +';' as 禁用约束 from ...

  6. SQL入门语句之INSERT、UPDATE和DELETE

    一.SQL入门语句之INSERT insert语句的功能是向数据库的某个表中插入一个新的数据行 1.根据对应的字段插入相对应的值 insert into table_name(字段A, 字段B, 字段 ...

  7. iOS 编码转换

    - (NSString *)SaveFileToDocuments:(NSString *)url { // NSString *url = @"http://172.28.250.70/a ...

  8. LeetCode之387. First Unique Character in a String

    -------------------------------------------------- 最开始的想法是统计每个字符的出现次数和位置,如下: AC代码: public class Solu ...

  9. python常用文件处理函数_1

    1.range()函数 函数原型:range(start,end,scan) 参数含义:start:计数从start开始.默认是从0开始的,如range()等价于range(0,5)  end:计数到 ...

  10. iOS ---不一样的NSLog打印(精准打印)

    在iOS开发过程中,调试是很重要的过程,而除了各种断点调试(普通断点.条件断点.全局断点)之外,似乎NSLog是我们调试最常用的方法,当然,也是最简单朴素的寻debug方法. 在项目中,我们常使用的N ...