使用tableView和CollectionView的时候需要注意的几点(单元格重用)


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];




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值