简体   繁体   English

UICollectionView单元格在滚动时消失

[英]UICollectionView cell disappears on scroll

I have two collectionViews. 我有两个collectionViews。 A collectionViewController and another collectionView in the header. 一个collectionViewController和标题中的另一个collectionView。 When I scroll down the collectionViewController, the collectionView cells of the the header collectionView disappear. 当我向下滚动collectionViewController时,标题collectionView的collectionView单元格消失。 There is also a segControl in the header that changes the collectionViewController cells and this also makes the header collectionView cells disappear. 标头中还有一个segControl,用于更改collectionViewController单元格,这也使标题collectionView单元格消失。 When the controller appears all the cells are present, BUT when i wither scroll of select the segControl the header Collection View cells disappear. 当控制器出现所有单元格都存在时,但是当我滚动选择segControl时,标题集合视图单元格消失。 The collectionViewController works fine, just the one in the header is messed up. collectionViewController工作正常,只是标题中的那个搞砸了。

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

} }

The Seg Method: Seg方法:

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

} }

it's late but this will help for others 现在已经晚了,但这对其他人有帮助

i also had similar problem , i just replaced [self.myCollectionView reloadData] with [self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]]; 我也有类似的问题,我只是用[self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];替换[self.myCollectionView reloadData] [self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]]; to refresh the collectionview and it shows all cells, you can try it. 要刷新集合视图并显示所有单元格,您可以尝试一下。

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

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