UIView * lineView = [[ UIView alloc]init];
lineView.backgroundColor= [ UIColor blackColor];
[self.contentView addSubview:lineView];
// self.lineView = lineView;
// 实现这个方法之后, layout.itemSize 就会失效.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//获取模型数据
LZJNewsChannelModel * channel = self.channels[indexPath.item];
// cell 的宽高. cell 的宽度等于 大 label(选中状态) 的宽
CGSize itemSize = [self getSizeWithTname:channel.tname];
return itemSize;
}
//设置大lable的大小
-(CGSize)getSizeWithTname:(NSString *)tName{
UILabel * lable = [[ UILabel alloc]init];
lable.text = tName;
lable.font = [ UIFont systemFontOfSize:18];
//自适应高度
[lable sizeToFit];
return lable.bounds.size;
}
// animated: YES ,会有动画效果,滚动中间所有的数据都会加载出来.
// animated: NO ,不会有动画效果,只会加载滚动结束之后位置的数据.
[ self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];