UICollectionView 单元格在滚动时消失

UICollectionView cell disappears on scroll

我有两个 collectionView。 header 中的一个 collectionViewController 和另一个 collectionView。当我向下滚动 collectionViewController 时,header collectionView 的 collectionView 单元格消失了。 header 中还有一个 segControl 可以更改 collectionViewController 单元格,这也会使 header collectionView 单元格消失。当控制器出现时,所有单元格都存在,但是当我滚动 select segControl 时,header Collection 视图单元格消失。 collectionViewController 工作正常,只是 header 中的那个被搞砸了。

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (collectionView == self.collectionView) {
    return [self.dataArray count];
}
return [self.groupArray count];
}

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

if (collectionView != self.collectionView) {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"socialCell" forIndexPath:indexPath];
    PFObject *group = [self.groupArray objectAtIndex:indexPath.row];
    UILabel *label = (UILabel *) [cell viewWithTag:55];
    label.text = [group objectForKey:@"Title"];
    return cell;
}

    userPostCell *postCell = (userPostCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"postCell" forIndexPath:indexPath];
    userPictureCell *pictureCell = (userPictureCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureCell" forIndexPath:indexPath];
    userEventCell *eventCell = (userEventCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"eventCell" forIndexPath:indexPath];

    PFObject *temp = [self.dataArray objectAtIndex:indexPath.row];
    if (self.dataArray == self.postsArray) {

        postCell.postLabel.text = [temp objectForKey:@"stringPost"];
        [self changeToCircle:postCell.profileImage];
        if ([temp objectForKey:@"Event"] == nil) {
            [postCell.noEventNameButton setTitle:[temp objectForKey:@"User_Name"] forState:UIControlStateNormal];
            postCell.noEventNameButton.tag = indexPath.row;
            postCell.nameButton.hidden = true;
            postCell.noEventNameButton.hidden = false;
        }
        else{
            [postCell.nameButton setTitle:[temp objectForKey:@"User_Name"] forState:UIControlStateNormal];
            postCell.nameButton.tag = indexPath.row;
            postCell.noEventNameButton.hidden = true;
            postCell.nameButton.hidden = false;

            //[postCell.eventButton addTarget:self action:@selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];

        }
        [postCell.eventButton setTitle:[temp objectForKey:@"Event"] forState:UIControlStateNormal];

        NSString *createdTime = [NSDateFormatter localizedStringFromDate: temp.createdAt dateStyle: NSDateFormatterNoStyle timeStyle: NSDateFormatterShortStyle];
        [postCell.timeButton setTitle:createdTime forState:UIControlStateNormal];

        PFFile *imageFile = [temp objectForKey:@"profileImage"];
        NSData *data = [imageFile getData];
        postCell.profileImage.image = [UIImage imageWithData:data];

        NSArray *likeArray = [temp objectForKey:@"likes"];
        NSString *likeString = [NSString stringWithFormat:@"%lu Likes", (unsigned long)likeArray.count];
        if ([likeArray containsObject:[PFUser currentUser].objectId]) {
            postCell.likeButton.hidden = true;
        }
        else{
            postCell.likeButton.hidden = false;
        }

        NSArray *commentArray = [temp objectForKey:@"Comments"];
        NSString *commentString = [NSString stringWithFormat:@"%lu Comments", (unsigned long)commentArray.count];

        [postCell.likesButton setTitle:likeString forState:UIControlStateNormal];
        [postCell.commentsButton setTitle:commentString forState:UIControlStateNormal];

        postCell.eventButton.tag = indexPath.row;
        postCell.likeButton.tag = indexPath.row;
        postCell.commentsButton.tag = indexPath.row;
        postCell.likesButton.tag = indexPath.row;

        [postCell.likesButton addTarget:self action:@selector(likesPage:) forControlEvents:UIControlEventTouchUpInside];
        [postCell.likeButton addTarget:self action:@selector(like:) forControlEvents:UIControlEventTouchUpInside];
        [postCell.commentsButton addTarget:self action:@selector(commentsPage:) forControlEvents:UIControlEventTouchUpInside];
        [postCell setUserInteractionEnabled:YES];
        return postCell;

    }
    if (self.dataArray == self.personImages) {
        UIImage *personImage = [self.dataArray objectAtIndex:indexPath.row];
        pictureCell.userImage.image = personImage;
        return pictureCell;
    }
    if (self.dataArray == self.eventsArray) {
        [eventCell.userEvent setTitle:[temp objectForKey:@"Title"] forState:UIControlStateNormal];
        eventCell.userEvent.tag = indexPath.row;
        [eventCell.userEvent addTarget:self action:@selector(eventPage:) forControlEvents:UIControlEventTouchUpInside];

        [eventCell.userGroup setTitle:[temp objectForKey:@"Group_Name"] forState:UIControlStateNormal];
        eventCell.userGroup.tag = indexPath.row;
        [eventCell.userGroup addTarget:self action:@selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
        return eventCell;
    }
return postCell;

}

分段方法:

- (IBAction)segControl:(id)sender {
UISegmentedControl *segment = (UISegmentedControl *) sender;

if (segment.selectedSegmentIndex == 0) {
    self.dataArray = self.postsArray;
    [self.collectionView reloadData];
}
if (segment.selectedSegmentIndex == 1) {
    self.dataArray = self.personImages;
    [self.collectionView reloadData];
}
if (segment.selectedSegmentIndex == 2) {
    self.dataArray = self.eventsArray;
    [self.collectionView reloadData];
}

}

来晚了,但这对其他人有帮助

我也有类似的问题,我只是用[self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];替换了[self.myCollectionView reloadData]来刷新collectionview,它显示了所有单元格,你可以试试。