0
我試圖創建具有不同UICollectionViewCells 我有幾項內容UICollectionView,一個有3個的UILabel和UIImageView的,其他有2周的UIImageViews和6個UILables等顯示UICollectionView與多個不同類型的UICollectionViewCells
我是試圖初始化單元格併爲每個特定的場景添加視圖,但似乎並不奏效。 我正在檢查如果單元格==零,然後添加意見,(我得到'if'語句內)。
如果我使用 'addSubView' if語句裏面,它什麼都不做
ResumeFeedCell *cell= (ResumeFeedCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"ResumeFeedCell" forIndexPath:indexPath];
if (!cell) {
//init views in the cell and add them
UIImageView *mediaImage =[[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,530, 560)];
mediaImage.tag = 12;
[cell.contentView addSubview:mediaImage];
UIImageView *typeImage = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,150, 150)];
typeImage.tag = 11;
[cell.contentView addSubview:typeImage];
UILabel *textLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 530, 560)];
textLable.tag = 10;
[cell.contentView addSubview:textLable];
}
UIImageView *mediaImage = (UIImageView*)[cell.contentView viewWithTag:12];
UIImageView *typeImage = (UIImageView*)[cell.contentView viewWithTag:11];
UILabel *textLable = (UILabel*)[cell.contentView viewWithTag:10];
textLable.text = _bio;
textLable.textAlignment = NSTextAlignmentCenter;
textLable.numberOfLines = 0;
textLable.tag = 10;
textLable.adjustsFontSizeToFitWidth=YES;
textLable.minimumScaleFactor=0.5;
[mediaImage setImageWithURL:[NSURL URLWithString:getImageUrlwithIdAndSize([[[_datasource objectAtIndex:indexPath.row] objectForKey:@"media"] objectAtIndex:0], 1)]];
我嘗試了不同的方式,這樣的:
UIImageView *mediaImage = (UIImageView*)[cell.contentView viewWithTag:12];
if(!mediaImage)mediaImage = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,530, 560)];
mediaImage.tag = 12;
[cell.contentView addSubview:mediaImage];
UIImageView *typeImage = (UIImageView*)[cell.contentView viewWithTag:11];
if(!typeImage)typeImage = [[UIImageView alloc] initWithFrame:CGRectMake(0 , 0,150, 150)];
typeImage.tag = 11;
[cell.contentView addSubview:typeImage];
UILabel *textLable = (UILabel*)[cell.contentView viewWithTag:10];
if (!textLable)
textLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 530, 560)];
這似乎工作確定,但我認爲這有點醜陋和內存消耗。 ,因爲它增加了每個項目的子視圖,這使得它不可重用...
我看到THIS ANSWER但我想自定義cellForItemAtIndexPath。
任何幫助將不勝感激。 Tx, Shahar。