使用tableView和CollectionView的时候需要注意的几点(单元格重用)
2014-10-14 16:48
567 查看
1.创建单元格时————》要考虑到单元格的覆用问题
if (indexPath.section ==3) {
//公司概况
staticNSString *identify =@"HKJKCell";
HKJKCell *cell = [tableViewdequeueReusableCellWithIdentifier:identify];
if (cell ==nil) {
cell = [[[NSBundlemainBundle]loadNibNamed:@"HKJKCell"owner:niloptions:nil]lastObject];
cell.backgroundColor = [UIColorwhiteColor];
cell.backgroundView =nil;
}
cell.data =_gegudata;
return cell;
}
2.tableView和collectionView的协议方法中做操作时(特殊情况:请求网、创建视图(列:创建表头视图)、) 要考虑协议方法是否会多次调用。。。以至于不停的请求网络和创建视图(使得视图界面的数据不停的闪烁刷新 使得原视图被新创建的视图覆盖)
#pragma mark createSectionButton创建组视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//UIView *sectionView = nil;
if (section == 0) {
return nil;
}else if (section == 1) { //5个按钮
if (_sectionView1 == nil) {
_sectionView1 = [self createButtonWithSection:1];
}
return _sectionView1;
}else if (section == 2) { //3个按钮
if (_sectionView2 == nil) {
_sectionView2 = [self createButtonWithSection:2];
}
return _sectionView2;
}else {
if (_sectionView3 == nil) {
_sectionView3 = [self createButtonWithSection:3];
}
return _sectionView3;
}
}
3.collectionView布局对象来控制布局
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayoutalloc]init];
flowlayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
flowlayout.itemSize =CGSizeMake(kScreenWidth,kScreenHeight);//一般到协议方法中设置item的大小
flowlayout.minimumInteritemSpacing =0;
flowlayout.minimumLineSpacing =0;
self.collectionViewLayout = flowlayout;
self = [superinitWithFrame:framecollectionViewLayout:flowlayout];
if (self) {
self.backgroundColor = [UIColorclearColor];
self.backgroundView =nil;
self.dataSource =self;
self.delegate =self;
self.pagingEnabled =YES;
//注册单元格
identify =@"DetailCell";
[selfregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:identify];
}
return self;
4.collectionView单元格复用的问题
1: 首先创建一个类,继承UICollectionViewCell,将你这个TableView在个类的init方法里面去创建,并添加到self.contentView上
2:当数据传过来时复写set方法
- (void)setStockData:(NSDictionary *)stockData {
if (_stockData != stockData) {
_stockDetailpictureData = stockDetailpictureData;
[selfsetNeedsLayout];
}
}
3:再到layoutSubviews方法里填充(或传递数据)
- (void)layoutSubviews {
[superlayoutSubviews];
_detailTableView.stockData =self.stockData;
_detailTableView.code =self.code;
_detailTableView.backgroundColor = [UIColorclearColor];
//在init方法里创建时添加[self.contentViewaddSubview:_detailTableView];
}
4: 在回到控制器viewDidLoad方法里,注册重用 [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
5: 使用 MyCommentsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
5.collectionView和tableView滚动的方法从Section0(第0组)滚动的
NSIndexPath *indexpath = [NSIndexPath
indexPathForItem:_pageItem
inSection:0];
[self.collectionView
scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
if (indexPath.section ==3) {
//公司概况
staticNSString *identify =@"HKJKCell";
HKJKCell *cell = [tableViewdequeueReusableCellWithIdentifier:identify];
if (cell ==nil) {
cell = [[[NSBundlemainBundle]loadNibNamed:@"HKJKCell"owner:niloptions:nil]lastObject];
cell.backgroundColor = [UIColorwhiteColor];
cell.backgroundView =nil;
}
cell.data =_gegudata;
return cell;
}
2.tableView和collectionView的协议方法中做操作时(特殊情况:请求网、创建视图(列:创建表头视图)、) 要考虑协议方法是否会多次调用。。。以至于不停的请求网络和创建视图(使得视图界面的数据不停的闪烁刷新 使得原视图被新创建的视图覆盖)
#pragma mark createSectionButton创建组视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//UIView *sectionView = nil;
if (section == 0) {
return nil;
}else if (section == 1) { //5个按钮
if (_sectionView1 == nil) {
_sectionView1 = [self createButtonWithSection:1];
}
return _sectionView1;
}else if (section == 2) { //3个按钮
if (_sectionView2 == nil) {
_sectionView2 = [self createButtonWithSection:2];
}
return _sectionView2;
}else {
if (_sectionView3 == nil) {
_sectionView3 = [self createButtonWithSection:3];
}
return _sectionView3;
}
}
3.collectionView布局对象来控制布局
UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayoutalloc]init];
flowlayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
flowlayout.itemSize =CGSizeMake(kScreenWidth,kScreenHeight);//一般到协议方法中设置item的大小
flowlayout.minimumInteritemSpacing =0;
flowlayout.minimumLineSpacing =0;
self.collectionViewLayout = flowlayout;
self = [superinitWithFrame:framecollectionViewLayout:flowlayout];
if (self) {
self.backgroundColor = [UIColorclearColor];
self.backgroundView =nil;
self.dataSource =self;
self.delegate =self;
self.pagingEnabled =YES;
//注册单元格
identify =@"DetailCell";
[selfregisterClass:[UICollectionViewCellclass]forCellWithReuseIdentifier:identify];
}
return self;
4.collectionView单元格复用的问题
1: 首先创建一个类,继承UICollectionViewCell,将你这个TableView在个类的init方法里面去创建,并添加到self.contentView上
2:当数据传过来时复写set方法
- (void)setStockData:(NSDictionary *)stockData {
if (_stockData != stockData) {
_stockDetailpictureData = stockDetailpictureData;
[selfsetNeedsLayout];
}
}
3:再到layoutSubviews方法里填充(或传递数据)
- (void)layoutSubviews {
[superlayoutSubviews];
_detailTableView.stockData =self.stockData;
_detailTableView.code =self.code;
_detailTableView.backgroundColor = [UIColorclearColor];
//在init方法里创建时添加[self.contentViewaddSubview:_detailTableView];
}
4: 在回到控制器viewDidLoad方法里,注册重用 [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
5: 使用 MyCommentsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
5.collectionView和tableView滚动的方法从Section0(第0组)滚动的
NSIndexPath *indexpath = [NSIndexPath
indexPathForItem:_pageItem
inSection:0];
[self.collectionView
scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally
animated:YES];
相关文章推荐
- 使用UIPickerView需要注意的问题时候
- ARC下使用MJRefresh时需要注意tableView循环引用
- Java使用HashMap进行遍历时候需要注意的几点
- 源码推荐(0709):新闻框架scroll上重用三个tableView,collectionview的使用
- 关于使用READ TABLE语句的几点注意事项
- 使用SqlDataAdapter时,需要注意的几点
- 使用jQuery Ajax功能的时候需要注意的一个问题
- 使用jQuery Ajax功能的时候需要注意的一个问题
- 旧的代码在2005下使用需要注意以下几点
- C++使用boost python开发后发布程序需要注意的几点
- 使用jQuery Ajax功能的时候需要注意的一个问题
- 使用NOSQL的MongoDB建立索引时需要注意的几点建议和Explain优化分析
- 使用jQuery Ajax功能的时候需要注意的一个问题
- 使用php apc模块需要注意的几点
- 《转》使用jQuery Ajax功能的时候需要注意的一个问题
- 关于使用READ TABLE语句的几点注意事项...(原文来源于网络)
- 在使用动软.net代码生成器生成access数据库的时候需要注意的
- 使用switch语句需要注意的几点
- 用SPQuery查询SPList的时候需要注意的几点
- foreach语句使用需要注意的几点