【问题标题】:Scrolling horizontal and vertical both direction水平和垂直两个方向滚动
【发布时间】:2013-06-06 06:15:48
【问题描述】:

我需要水平和垂直滚动图像。

我的部分将segregated vertically 和in each section itself it will be so many images which will be scrolled horizontally。

我使用UICollectionView 和Ray wenderlich tutorial 进行了垂直滚动,但是如何在每个部分中进行水平滚动?

我是否应该为此使用任何其他对象,例如 UITableView 或 UIScrollview?

任何可用的教程如何实现这一点?

Is it compulsory to implement Layout subclass or UICollectionviewFlowlayout when using collection view?

编辑

到目前为止,我已经实现了这个垂直滚动的代码,每个部分有 5 个部分,但不能滚动各个部分

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    NSArray *arrayReturn = [self returnArray:section];
    return arrayReturn.count;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"imagecell" forIndexPath:indexPath];

    NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
        NSArray *arrSection = [NSArray arrayWithArray:[self returnArray:indexPath.section]];
        NSString *fileName = [arrSection objectAtIndex:indexPath.row];
        UIImage *image = [UIImage imageNamed:fileName];

           dispatch_async(dispatch_get_main_queue(), ^{
//               cell.imgVw.image = ;
               if([CollectionView.indexPathsForVisibleItems containsObject:indexPath]){
                   ImageCell *currentCell = (ImageCell *) [cv cellForItemAtIndexPath:indexPath];
                   currentCell.imgVw.image = image;
               }

           });
    }];

    [self.thumbnailQueue addOperation:operation];

    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{

    HeaderView *Header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PhotoHeaderView" forIndexPath:indexPath];

    Header.lblHeaderTitle.text = @"Header title";

    Header.backgroundColor = [UIColor yellowColor];
    return Header;
}

【问题讨论】:

  • 任何能准确解释您的要求的屏幕截图?
  • 它都是基于你的ScrollView的ContentSize,所以你的ScrollView有五个合适的contentSize并且给UIScrollView的方向锁。

标签: iphone ios objective-c uiscrollview uicollectionview


【解决方案1】:

这是一个类似的教程:

http://www.raywenderlich.com/4723/how-to-make-an-interface-with-horizontal-tables-like-the-pulse-news-app-part-2

基本上你需要做以下事情:

  • 获取 tableView 或 CollectionView
  • 您需要在每个单元格中添加启用水平滚动的滚动视图。
  • 在创建单元格时将项目添加到每个单元格的滚动视图,并为滚动视图提供适当的内容大小。

它会给你一个想要的结果。

【讨论】:

  • 为每个单元格添加滚动视图会导致内存问题
  • 它会增加视图的内存使用量,但我认为这不会导致任何内存问题。这是使用内部具有某些视图的自定义单元格的类似情况。
  • 但是如果我可以使用集合视图会更好,因为它会在方向改变时自动管理单元格的高度和宽度
  • 正如我在回答中所说,您也可以使用相同的方法使用集合视图。它也可以基于单元格工作。
  • 本教程帮助了我,但我将它实现为 tableview,每个单元格都有 collectionview 以水平方向滚动。这工作正常。
【解决方案2】:

试试这个

**viewController.h**

IBOutlet UIScrollView *scrollViewMain;


**viewController.m**

- (void)viewDidLoad
{
   [super viewDidLoad];
   [self createDisplay];
// Do any additional setup after loading the view, typically from a nib.
}

-(void) createDisplay
{
for(UIView *subView in scrollViewMain.subviews)
{
    if(![subView isKindOfClass:[UIImageView class]])
    {
        [subView removeFromSuperview];
    }
}
int yPos =5;

for (int i=0; i< 10; i++)
{

    int xPosition=5;


    UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(xPosition, yPos, scrollViewMain.frame.size.width, 100)];
    scrollView.backgroundColor =[UIColor clearColor];
    scrollView.autoresizingMask =UIViewAutoresizingFlexibleWidth;



    xPosition +=5;

    for (int j=0; j<10; j++)
    {

        UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, 0, 100, 100)];
        lblTitle.textColor = [UIColor whiteColor];
        lblTitle.backgroundColor =[UIColor greenColor];
        lblTitle.text = @"test";
        lblTitle.numberOfLines =0;
        lblTitle.font = [UIFont boldSystemFontOfSize:15];

        [scrollView addSubview:lblTitle];

        xPosition +=100;
        xPosition +=10;
    }
    [scrollView setContentSize:CGSizeMake(xPosition, 100)];
    [scrollViewMain addSubview:scrollView];
    yPos +=120;
}
[scrollViewMain flashScrollIndicators];
[scrollViewMain setContentSize:CGSizeMake(0, yPos)];
  }

【讨论】:

    猜你喜欢
    • 2014-07-12
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 2015-05-31
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多