简体   繁体   English

自定义UICollectionViewCell不显示更改

[英]Custom UICollectionViewCell not showing changes

I have created class that extends UICollectionViewCell and cell was customised within the storyboard. 我创建了扩展UICollectionViewCell类,并且在情节UICollectionViewCell单元进行了自定义。 Within the initWithCoder for the cell I modify the labels shadow. 在单元格的initWithCoder中,我修改了标签阴影。

    - (id)initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
        self.artistNameLabel.font = [UIFont fontWithName:@"Montserrat-Regular" size:12];
        self.artistNameLabel.layer.masksToBounds = NO;
        self.artistNameLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.65];
        self.artistNameLabel.layer.shadowOffset = CGSizeMake(0, 0);
        self.artistNameLabel.layer.shadowOpacity = 1.0;
        self.artistNameLabel.layer.shadowRadius = 2.0;
        return self;
    }

For some reasons the changes do not show up, even though the init is called when creating the cell. 由于某些原因,即使创建单元时调用了init,更改也不会显示出来。

If I move the shadow code into -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath it shows fine, but obviously this is not very good for reuse. 如果我将影子代码移到-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath它会很好显示,但是显然这对于​​重用不是很好。

Move it over to - (void)awakeFromNib . 将其移至- (void)awakeFromNib That's a much better place to put it. 那是个好得多的地方。

Make a single public method on your custom cell called something like updateUI and call that from within your -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath method. 在自定义单元格上创建一个名为updateUI类的公共方法,然后从-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath方法中-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath方法。 You can alloc init your collection view cell's var's in your - (id)initWithCoder:(NSCoder *)aDecoder method, but to have your shadowRadius, shadowOpacity, etc go into effect I recommend the approach described above. 您可以在- (id)initWithCoder:(NSCoder *)aDecoder方法中- (id)initWithCoder:(NSCoder *)aDecoder初始化集合视图单元格的var,但是要使shadowRadius,shadowOpacity等生效,我建议使用上述方法。

#pragma mark - Helpers

- (void)updateUI // Update data within alreay alloc inited UI elements
{
    self.backgroundColor = [UIColor redColor];
    self.myImageView.frame = self.contentView.frame;
    self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
}

- (void)setup // Alloc init UI elements
{
    self.myImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.myImageView];
}

#pragma mark - Lifecycle Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

For answer on your i need little bit more information but check next 有关您的答案,我需要更多信息,但请检查下一个

1) Are you using your custom UICollectionViewCell 1)您是否使用自定义的UICollectionViewCell

在此处输入图片说明

2) In method 2)方法

   - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView   cellForItemAtIndexPath:(NSIndexPath *)indexPath {
   DomyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DomyCell" forIndexPath:indexPath];

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

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