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

时间:2016-10-12 10:45:54

标签: ios objective-c iphone scroll uicollectionview

我的问题如下: 我创建了一个UICollectionViewController,它应该在单独的自定义单元格中显示以前下载的文本内容(参见附件截图)。但是在向下滚动之后,单元格不再是相同的顺序。我还检查了索引路径...我认为应用程序将错误的单元格返回到错误的索引路径
After launching
After scrolling [Wrong Content AND Wrong Order]

继承我的代码

- (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;
 }

子类功能

 -(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];

    }

}

有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

在指定initWithFrame的单元格中,在

中编写代码

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

这将使用子视图初始化单元格,您无需调用上述自定义方法。还要为子视图创建属性。

您命名方法并打算使用它的方式也令人困惑和错误。之后我建议:

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

使用单元格的属性清除每个子视图的内容。然后从词典中添加您想要的内容。希望这有帮助