我试图使用nib在我的自定义UICollectionViewCell中重新定位UILabel。 问题在于,一开始,只有第一个项目的子视图看起来应该(按代码定位),而所有其他项子视图的定位都像笔尖一样。 如果我来回滚动几次,它也会加载所有其他项目。
我尝试使用和不使用AutoLayout以及在某些答案HERE中说明并尝试按照here
设置/删除约束失去理智,任何帮助都会很棒!
在我的FeedCell中我这样做了:
-(void)layoutSubviews{
[super layoutSubviews];
[self sortViews];
}
- (void) sortViews{
[_fromLable sizeToFit];
[_toLable sizeToFit];
[_fromYearLabel sizeToFit];
[_dashLabel sizeToFit];
[_dashLabel centerHorizontalInParent];
// rotate the year labels
[_fromYearLabel setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[_toLable setX:[_dashLabel x] + [_dashLabel width] + 15 ];
[_fromYearLabel setX:[_dashLabel x] - [_fromYearLabel width] - 15 ];
}
我尝试在cellForItemAtIndexPath和willDisplayCell中调用[cell setNeedsDisplay]:forItemAtIndexPath
我的单元格在cellForItemAtIndexPath中出列如下:
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FeedCell *cell= (FeedCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"FeedCell" forIndexPath:indexPath];
NSDictionary *feed = [_datasource objectAtIndex:indexPath.row];
if (feed) {
[cell.mediaImage setHidden:NO];
NSString* desc = [feed objectForKey:@"desc"];
if(desc){
cell.descriptionTextview.text = desc;
[cell.descriptionTextview adjustFontSizeToFillItsContentWithMaxFontSize:80 andMinValue:8 WithFont:fMyriadProRegular];
[cell.descriptionTextview setHidden:NO];
}
else
[cell.descriptionTextview setHidden:YES];
if ([feed objectForKey:@"title"]) {
cell.titleLabel.text = [feed objectForKey:@"title"];
}
if ([feed objectForKey:@"place"]) {
cell.placeLabel.text = [feed objectForKey:@"place"];
}
if ([feed objectForKey:@"location"]) {
[cell.locationLable setHidden:NO];
cell.locationLable.text = [feed objectForKey:@"location"];
}else{
[cell.locationLable setHidden:YES];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
NSInteger yearStart = 0;
NSInteger yearend = 0;
NSInteger totalYears = 0;
if ([feed objectForKey:@"startTime"]) {
NSString* startTime = [feed objectForKey:@"startTime"];
dateFromString = [dateFormatter dateFromString:startTime];
cell.fromLable.text = [dateFromString monthStringWithLength:3 capital:YES];
yearStart = [dateFromString year];
}
if ([feed objectForKey:@"endTime"]) {
NSString* endTime = [feed objectForKey:@"endTime"];
dateFromString = [dateFormatter dateFromString:endTime];
cell.toLable.text = [dateFromString monthStringWithLength:3 capital:YES];
yearend = [dateFromString year];
}
if (yearStart == 0 && yearend == 0) {
[cell.totalYears setHidden:YES];
}else if (yearStart > 0 && yearend == 0){
[cell.totalYears setHidden:NO];
totalYears = [[NSDate date] year] - yearStart;
}else{
[cell.totalYears setHidden:NO];
totalYears = yearend - yearStart;
}
cell.fromYearLabel.text = stringWithFormat(@"%zd", yearStart);
cell.totalYears.text = stringWithFormat(@"(%zd YEARS)", totalYears);
[cell.mediaImage setImageWithURL:[NSURL URLWithString:getImageUrlwithIdAndSize([[[_datasource objectAtIndex:indexPath.row] objectForKey:@"media"] objectAtIndex:0], 1)]];
}
cell.backgroundColor= [UIColor whiteColor];
[cell setNeedsDisplay];
return cell;
}
答案 0 :(得分:0)
请尝试以下代码:
(void)awakeFromNib
{
[super awakeFromNib];
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}