简体   繁体   English

UICollectionView滚动后显示错误的单元格顺序

[英]UICollectionView displays wrong cell order after scrolling

My problem is the following: I created a UICollectionViewController that should display previous downloaded text-content in separate custom cells (see attached screenshots). 我的问题如下:我创建了一个UICollectionViewController,该UICollectionViewController应该在单独的自定义单元格中显示以前下载的文本内容(请参阅随附的屏幕截图)。 But after scrolling down, the cells are not in the same order anymore. 但是向下滚动后,单元格不再处于相同的顺序。 I also checked, the indexpath... I think that the App returns the wrong cells to the wrong index path.row 我还检查了indexpath ...我认为应用程序将错误的单元格返回到错误的index path.row
After launching 发射后
After scrolling [Wrong Content AND Wrong Order] 滚动后[错误的内容和错误的顺序]

Heres my code for the 这是我的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

      TextCollectionViewCell *textCell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"textCell" forIndexPath:indexPath];
      //CURRENT DATA
      NSLog(@"Row: %li - Item:%li",(long)indexPath.row,(long)indexPath.item);
      NSDictionary *currentDataDict = nil;
      currentDataDict = [[_feedItemsArray objectAtIndex:indexPath.item]copy];
      NSLog(@"%@",[currentDataDict valueForKey:@"id"]);
      if ([textCell.subviews count] <=2) {
            [textCell initCell:[currentDataDict valueForKey:@"text"] time:NULL username:[currentDataDict valueForKey:@"admin"]];
      }
      [textCell.btn_title addTarget:self
                                   action:@selector(goToProfile)
                         forControlEvents:UIControlEventTouchUpInside];

      return textCell;
 }

And the Subclass function 和子类功能

 -(void)initCell:(NSString*)content time:(NSDate*)time username:(NSString*)username` in the `TextCollectionViewCell`-Class:

    self.backgroundColor = [UIColor clearColor];


    UIImageView *profileImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width*0.13, self.frame.size.width*0.13)];
    profileImg.image = [UIImage imageNamed:@"prfImg.png"];
    profileImg.layer.cornerRadius = profileImg.frame.size.height /2;
    profileImg.layer.masksToBounds = YES;
    profileImg.layer.borderWidth = 3;
    profileImg.layer.borderColor = [UIColor whiteColor].CGColor;


    self.btn_title = [[UIButton alloc]initWithFrame:CGRectMake(50, 2, self.frame.size.width*0.5, self.frame.size.height *0.1)];
    [self.btn_title setTitle: [NSString stringWithFormat:@"@%@",username] forState:UIControlStateNormal];
    [self.btn_title setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [self.btn_title setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    //  [lbl_title setFont:[UIFont fontWithName:@"Helvetica Neue-CondensedBold" size:14]];


    UILabel *lbl_time = [[UILabel alloc]initWithFrame:CGRectMake(50, 20, self.frame.size.width*0.5, self.frame.size.height *0.1)];
    lbl_time.text = @"Gerade eben";
    [lbl_time setFont:[UIFont systemFontOfSize:12]];
    lbl_time.textColor = [UIColor grayColor];

    self.lbl_main = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height*0.7)];
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    self.lbl_main.font = [UIFont fontWithName:@"Helvetica" size:15.0];
    [self.lbl_main setPreferredMaxLayoutWidth:280.0];
    self.lbl_main.numberOfLines = 0;
    CGSize maximumLabelSize = CGSizeMake(self.frame.size.width, self.frame.size.height);

    CGSize expectedLabelSize = [content sizeWithFont:self.lbl_main.font constrainedToSize:maximumLabelSize lineBreakMode:self.lbl_main.lineBreakMode];
    //adjust the label the the new height.
    float height = lbl_time.frame.origin.y+lbl_time.frame.size.height;
    CGRect newFrame = CGRectMake(self.frame.size.width*0.1, height , self.frame.size.width*0.85, self.frame.size.height*0.6);
    self.lbl_main.frame = newFrame;
    self.lbl_main.text = content;
    self.lbl_main.textColor = [UIColor whiteColor];
    //lbl_main.backgroundColor = [UIColor redColor];
    float heightBTN = self.frame.size.width*0.16;

    self.cont_text = [[UIView alloc]initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-heightBTN-50)];
    self.cont_text.backgroundColor = [UIColor colorWithRed:0.86 green:0.32 blue:0.13 alpha:1.0];
    self.cont_text.layer.cornerRadius = 8;
    self.cont_text.layer.masksToBounds = YES;
    self.lbl_main.center = CGPointMake(self.cont_text.frame.size.width*0.5, self.cont_text.frame.size.height*0.5);

    float btnX = self.frame.size.width-heightBTN*1.1;
    float btnY = self.cont_text.frame.size.height+self.cont_text.frame.origin.y*0.5;

    UIButton *btn_like = [[UIButton alloc]initWithFrame:CGRectMake(btnX,btnY,heightBTN , heightBTN)];
    [btn_like.layer setMasksToBounds:YES];
    [btn_like.layer setCornerRadius:heightBTN*0.5];
    btn_like.backgroundColor = [UIColor colorWithRed:1.00 green:0.69 blue:0.23 alpha:1.0];

    UIImageView *img_like = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, btn_like.frame.size.width*0.5, btn_like.frame.size.width*0.5)];
    img_like.image = [UIImage imageNamed:@"like.png"];
    img_like.center = CGPointMake(btn_like.frame.size.width*0.5, btn_like.frame.size.height*0.5);
    [btn_like addSubview:img_like];
    NSLog(@"%@",self.subviews);
    if ([self.subviews count] <=2) {
        [self addSubview:self.cont_text];
        [self addSubview:btn_like];
        [self addSubview:lbl_time];
        [self addSubview:self.btn_title];
        [self addSubview:profileImg];
        [self.cont_text addSubview:self.lbl_main];

    }

}

Can somebody help me? 有人可以帮我吗?

In the cell designated initWithFrame write the code in your 在指定为initWithFrame的单元格中,将代码写入

-(void)initCell:(NSString*)content time:(NSDate*)time username:(NSString*)username

This will initialize the cell with the subviews, you need without calling your above mentioned custom method. 这将使用子视图初始化单元格,而无需调用上述自定义方法。 Also create properties for the subviews. 还为子视图创建属性。

The way you have named the method and intend to use it is also confusing and wrong. 您命名方法并打算使用它的方式也令人困惑和错误。 After that I would suggest in: 之后,我建议:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

clear out the content of each subview by using the properties of the cell. 通过使用单元格的属性清除每个子视图的内容。 Then add the content you wish from the dictionary. 然后从字典中添加所需的内容。 Hope this helps 希望这可以帮助

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

相关问题 当异步附加新项目时,UICollectionView快速滚动在单元格中显示错误的图像 - UICollectionView fast scrolling displays wrong images in the cell when new items are appended asynchronously 在 UICollectionView 中滚动 2 - 3 次后单元格消失 - Cell disappearing after scrolling 2 - 3 times in UICollectionView UICollectionView 开始滚动后更改单元格大小 - UICollectionView Change Cell size after start scrolling 当当前单元格不可见时,UICollectionView显示错误的项目数吗? - UICollectionView displays wrong number of items when current cell is not visible? UICollectionView:单元格删除后滚动到带有动画的最后一个单元格时出现故障 - UICollectionView: glitches while scrolling to the last cell with animation after cell deletion 滚动到 UICollectionView 中的下一个单元格 - Scrolling to next cell in UICollectionView UICollectionView - 滚动到第一个单元格 - UICollectionView - Scrolling to first cell UICollectionView在单元格中显示错误的图像 - UICollectionView displays wrong images in cells 页面加载后滚动后如何访问UICollectionView中的特定单元格? - How to access specific cell in UICollectionView after scrolling when page loads? 滚动后的Swift UICollectionView单元格“收藏夹”按钮更改为“收藏夹” - Swift UICollectionView Cell favourite button after scrolling changed to unfavorite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM