基本上,我试图获得类似于本教程结果的内容:http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell
我很想让它按照我想要的方式工作......我的目标是:
将collectionViewCell从教程更改为自定义单元格,包含例如UILabel ......
我需要更改哪些代码?我尝试在以下几行中更改代码(来自AFTableViewCell.h),但还没有让它工作......
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing= 100000.0f;
layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 0);
layout.itemSize = CGSizeMake(40, 22);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor blackColor];
self.collectionView.showsHorizontalScrollIndicator = YES;
[self.contentView addSubview:self.collectionView];
return self;
}
我想在每个标签上添加相应的tableviewcell + collectionviewcell数字;
我的想法是(原谅我对iOS的了解不多......我刚开始):
更改自定义单元格的registerClass方法:
[self.collectionView registerClass:[MyCustomCollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
在其.h文件中使用MyCustomCollectionViewCell代码:
@interface MyCustomCollectionViewCell : UICollectionViewCell
@property (strong, nonatomic) UILabel *simpleLabel;
@end
然后,我想我只需要将以下内容添加到教程中,设置每个UICollectionViewCell背景的背景(AFViewController.m):
-(MyCustomCollectionViewCell *)collectionView:(AFIndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
NSArray *collectionViewArray = self.colorArray[collectionView.index];
cell.backgroundColor = collectionViewArray[indexPath.item];
cell.simpleLabel.text = @"hi";
return cell;
}
但是,没有标签显示...(只是为了便于测试,只是试着让它显示“hi”)
答案 0 :(得分:2)
在此问题的帮助下修复:Programmatically Creating UILabel
不需要自定义CollectionViewClass,只需在AFViewController.m中编辑以下方法:
-(UICollectionViewCell *)collectionView:(AFIndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
NSArray *collectionViewArray = self.colorArray[collectionView.index];
cell.backgroundColor = collectionViewArray[indexPath.item];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,40,22)];
fromLabel.text = @"hi";
fromLabel.numberOfLines = 1;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.adjustsLetterSpacingToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textColor = [UIColor blackColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[cell addSubview:fromLabel];
return cell;
}