繁体   English   中英

在自定义UICollectionViewCell内调整UILabel的大小并重新放置

[英]Resizing and re-positioning UILabel inside custom UICollectionViewCell

我试图用笔尖在我的自定义UICollectionViewCell中重新放置UILabel。 问题是,一开始,只有第一个项目的子视图看起来应该是应该的(通过代码放置),而所有其他项目的子视图都像笔尖一样定位。 如果我来回滚动几次,它也会加载所有其他项目。

我尝试使用和不使用AutoLayout以及在此处的一些答案中进行了说明,并尝试按此处所述设置/删除约束

放心,任何帮助都会很棒!

在我的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:forItemAtIndexPath中调用[cell setNeedsDisplay]

我的单元格在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;
    }

尝试以下代码:

(void)awakeFromNib
{
[super awakeFromNib];

self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM