您的位置:首页 > 其它

使用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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: