【问题标题】:UICollectionView with Paging, each cell covers whole screen, issue when drag but not goes to next or previous cell yet带有分页的 UICollectionView,每个单元格覆盖整个屏幕,拖动时发出但尚未转到下一个或上一个单元格
【发布时间】:2014-08-02 07:06:46
【问题描述】:

启用分页的 UICollectionView 出现问题。每个单元格将覆盖整个屏幕,因此一次只能看到 1 个单元格 在主视图中我有 UICollectionView,当用户滑动到其他单元格时,导航标题将根据新单元格更改,这在用户正确滑动时完美运行,这意味着当滑动时,UICollectionView 将整个新单元格推送到屏幕 但是,当用户轻扫一下以显示下一个单元格,然后返回当前显示的单元格时,导航标题会更改,但内容仍然是当前的 有谁知道如何解决这个问题? 我在这里附上图片以供说明

非常感谢

这是我的代码:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SingleLabViewCollectionScrollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];
    if (cell == nil){
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SingleLabViewCollectionScrollCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // hide
    [cell.viewMain2 setHidden:YES];
    [cell.viewMain3 setHidden:YES];
    [cell.viewBubble2 setHidden:YES];
    [cell.viewBubble3 setHidden:YES];

    // set text view
    [cell.lblLabText setFont:FONT_AVANT_BOOK(cell.lblLabText.font.pointSize)];
    [cell.textview setScrollEnabled:YES];
    [cell.textview setUserInteractionEnabled:YES];

    NSDictionary *thisDict = [dictLabContentPlist valueForKey:self.titleName];
    if(thisDict != nil){
        NSDictionary *thisDictContent = [thisDict objectForKey:@"Text"];
        NSString *content = [thisDictContent valueForKey:@"Content"];
        NSAttributedString *ctAttri = [self attributedMessageFromMessage:content];
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(messageTapped:)];
        [cell.textview addGestureRecognizer:gesture];
        cell.textview.attributedText = ctAttri;
        cell.lblLabText.text = [thisDictContent valueForKey:@"Title"];
    }

    // expand textview according to text
    [cell.textview sizeToFit];
    [cell.textview layoutIfNeeded];
    cell.textview.backgroundColor = [UIColor clearColor];

    CGRect rect = cell.viewDescription.frame;
    rect.size.height = cell.textview.contentSize.height + 40;
    cell.viewDescription.frame = rect;
    [cell.viewMain.layer setCornerRadius:5];
    [cell.viewMain2.layer setCornerRadius:5];
    [cell.viewMain3.layer setCornerRadius:5];
    [cell.viewDescription.layer setCornerRadius:5];

    // set scrollview contentsize
    rect.size.height += 400;
    cell.cellScrollView.contentSize = rect.size;
    [cell.cellScrollView setScrollEnabled:YES];
    [cell.cellScrollView scrollsToTop];

    // set bubble view
    [cell.btnSeeMore addTarget:self action:@selector(expand:) forControlEvents:UIControlEventTouchUpInside];

    // get data for this testID
    for(int i = 0; i < self.arrayTestIDs.count; i++){
        if([self.arrayTestIDs[i] isEqualToString:currentID]){
            currentIndex = i;
            break;
        }
    }

    // load 1st time
    if(firstTimeOpenThisView){
        cell.lblTestName.text = self.arrayTestNames[currentIndex];
        self.titleName = self.arrayTestNames[currentIndex];
        currentID = self.arrayTestIDs[currentIndex];
    }else{
        currentIndex ++;
        // set title name
        cell.lblTestName.text = self.arrayTestNames[indexPath.row];
        self.titleName = self.arrayTestNames[indexPath.row];
        currentID = self.arrayTestIDs[indexPath.row];
    }



    NSMutableDictionary *postData = [[NSMutableDictionary alloc] init];
    [postData setValue:singleton.ACCESS_TOKEN forKey:@"token"];
    [postData setValue:currentID forKey:@"testId"];

    // this is new block that check db when click next
    NSArray *fetchObjects = [singleton loadDataFromTable:@"TestData"];
    // first load data from DB
    isDataExist = false;
    for(NSManagedObject *item in fetchObjects){
        if ([[item valueForKey:@"testID"] isEqualToString:[NSString stringWithFormat:@"%@",currentID]]) {

            dataResponseDict = [item valueForKey:@"data"];
            NSDictionary *commonDict = [dataResponseDict objectForKey:@"Common"];
            cell.lblLeftValue.text = [commonDict valueForKey:@"min"];
            cell.lblLeftLevel.text = [commonDict valueForKey:@"statusBegin"];
            cell.lblUnit.text = [commonDict valueForKey:@"avg"];
            cell.lblRightLevel.text = [commonDict valueForKey:@"statusEnd"];
            cell.lblRightValue.text = [commonDict valueForKey:@"max"];

            dataResponseArray = [dataResponseDict objectForKey:@"LabReport"];

            //kenvu
            NSDictionary *testDict = [dataResponseArray[0] objectForKey:@"Test"];
            NSString *resultStr = [testDict valueForKey:@"result"];
            if([resultStr isKindOfClass:[NSNull class]])
                numberOfRows = 1;
            else
                numberOfRows = 2;

            [self loadDataIntoCell:cell indexPath:indexPath];
            isDataExist=true;

        }
    }

    if(!isDataExist){ // even data exist, still needs to download from server for latest labs data
        clvMain.hidden = YES;
        [self showHud];
        [ws downloadDataWithMethod:@"viewlab" requestMethod:@"POST" data:postData completionBlock:^(NSDictionary *resultDict){

            NSDictionary *tmpDict = [resultDict valueForKey:WS_RESULT_DATA];
            dataResponseDict = [resultDict valueForKey:WS_RESULT_DATA];
            if(dataResponseDict != NULL && ![dataResponseDict isKindOfClass:[NSNull class]] && dataResponseDict.count > 0){
                // remove existing data for this category id
                for(NSManagedObject *item in fetchObjects){
                    if ([[item valueForKey:@"testID"] isEqualToString:[NSString stringWithFormat:@"%@",currentID]]) {
                        [singleton deleteObjectFromDB:item];
                    }
                }
                // save data to db
                NSMutableDictionary *dataToSave = [[NSMutableDictionary alloc]init];
                [dataToSave setObject:[NSString stringWithFormat:@"%@",currentID] forKey:@"testID"];
                [dataToSave setObject:tmpDict forKey:@"data"];
                [singleton saveNewData:dataToSave forTable:@"TestData"];

                // only reload data if no data in DB
                if(!isDataExist){
                    NSDictionary *commonDict = [dataResponseDict objectForKey:@"Common"];
                    cell.lblLeftValue.text = [commonDict valueForKey:@"min"];
                    cell.lblLeftLevel.text = [commonDict valueForKey:@"statusBegin"];
                    cell.lblUnit.text = [commonDict valueForKey:@"avg"];
                    cell.lblRightLevel.text = [commonDict valueForKey:@"statusEnd"];
                    cell.lblRightValue.text = [commonDict valueForKey:@"max"];

                    dataResponseArray = [dataResponseDict objectForKey:@"LabReport"];
                    //kenvu
                    NSDictionary *testDict = [dataResponseArray[0] objectForKey:@"Test"];
                    NSString *resultStr = [testDict valueForKey:@"result"];
                    if([resultStr isKindOfClass:[NSNull class]])
                        numberOfRows = 1;
                    else
                        numberOfRows = 2;

                    [self loadDataIntoCell:cell indexPath:indexPath];
                }

            }else{ // if data == nil, still show static info
                numberOfRows = 1;
                cell.lblLeftValue.text = cell.lblLeftLevel.text = cell.lblUnit.text = cell.lblRightLevel.text = cell.lblRightValue.text = @"";
                [self loadDataIntoCell:cell indexPath:indexPath];
            }
            [self hideHud];
            clvMain.hidden = NO;
        }];
    }


    firstTimeOpenThisView = false;
    return cell;
}

-(void)loadDataIntoCell:(SingleLabViewCollectionScrollCell *)cell indexPath:(NSIndexPath*)indexPath{
    if(indexPath.row == self.arrayTestIDs.count - 1){
        btnNext.hidden = YES;
    }else{
        btnNext.hidden = NO;
    }
    if(indexPath.row == 0){
        btnPrevious.hidden = YES;
    }else{
        btnPrevious.hidden = NO;
    }

    // set navigation title
    titleView.lblTestName.text = self.titleName;
    [titleView.lblTestName setFont:FONT_AVANT_BOOK(cell.lblTestName.font.pointSize)];

    .....
}

【问题讨论】:

  • 向我们展示在滑动单元格时更改导航标题的代码。
  • 我只是更改了 cellForItemAtIndexPath 中的导航标题
  • 没有看到代码...我冒昧地猜测,一旦代理即将显示新单元格,它就会更改该方法中的标题。您可以做的是放置一个 if 语句来检查单元格在屏幕上的距离。如果过了一个不归路..设置标题。否则不要更改标题。
  • 你能告诉我怎么做吗?因为一旦用户开始拖动,UICollectionView 就已经调用了 cellForItemAtIndexPath。
  • 在您的问题中发布您的 cellForItemAtIndexPath 代码。让我们看看它到底在做什么,然后我可以根据你当前的代码确切地看到需要做什么。

标签: ios objective-c uicollectionview paging


【解决方案1】:

我找到了解决方案。 通过使用 scrollViewDidEndDecelerating,我只需在获取 UICollectionView 的 visibleCells 之前添加 0.1 秒的延迟,它总是返回正确的可见单元格

【讨论】:

    【解决方案2】:

    UICollectionView 继承自UIScrollView,这意味着您可以随时访问集合视图的contentOffset 属性。如果您将contentOffset 除以集合视图单元格的宽度,您将得出正在显示的单元格的索引,因此您可以确定当前可见的单元格。

    您还可以利用UIScrollViewDelegatescrollViewDidScroll 方法- 覆盖它,然后使用contentOffset 计算可见单元格移动了多远并触发导航栏文本的更改。因此,您可以等到当前单元格滚动超过其宽度的 50% 到下一个单元格后再进行更改 - 这样,如果用户触摸并让单元格“快速”返回,导航栏标题将不会改变.

    【讨论】:

      【解决方案3】:

      好的,因此查看 Apple 的开发人员文档会发现,确实没有任何委托方法可以告诉您何时可以在屏幕上看到单元格。我在cellForItemAtIndexPath 中检查这个的第一条评论在技术上也不准确。该方法不应该知道单元格在屏幕上的位置。只是如何显示它。

      所以我的建议是试试这个:

      cellForItemAtIndexPath 中删除对loadDataIntoCell 的方法调用。

      接下来由于UICollectionView 继承自UIScrollView,您可以使用这些委托方法来确定滚动时要执行的操作。这是一个粗略的例子:

      [collectionView visibleCells] 方法将为您提供数组中的所有可见单元格。你可以用它来改变你的标题:

      - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
          for (UICollectionViewCell *cell in [collectionView visibleCells]) {
              NSIndexPath *indexPath = [collectionView indexPathForCell:cell];
              NSLog(@"%@",indexPath);
             //Set the navigation title here 
          }
      }
      

      您只需要管理数组并确保其中的对象是正确的。但这应该让你朝着正确的方向前进。

      【讨论】:

      • 感谢您的建议。我刚刚尝试过,如果我像我的帖子中描述的那样滑动,[collectionView visibleCells] 有时会返回 2 个对象
      • 我认为这可能会发生。对此感到抱歉
      猜你喜欢
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多