UICollectionView单元格在滚动时消失

时间:2015-01-29 02:33:01

标签: ios xcode uicollectionview uicollectionviewcell

我有两个collectionViews。一个collectionViewController和标题中的另一个collectionView。当我向下滚动collectionViewController时,标题collectionView的collectionView单元格消失。标头中还有一个segControl,用于更改collectionViewController单元格,这也使标题collectionView单元格消失。当控制器出现所有单元格都存在时,但是当我滚动选择segControl时,标题集合视图单元格消失。 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;

}

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

}

1 个答案:

答案 0 :(得分:8)

它已经晚了,但这对其他人有帮助

我也有类似的问题,我只是将[self.myCollectionView reloadData]替换为[self.myCollectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];来刷新集合视图并显示所有单元格,您可以尝试。