YouTube就像iPad中的TableView,带有方向问题

时间:2012-02-10 16:24:58

标签: iphone

我明白为了实现这一点,需要创建自定义单元格。但是youtube tableview会在方向上重新排列。考虑到你必须将单元格出列,可以保持这些吗?

1 个答案:

答案 0 :(得分:1)

这是一个非常简单的GridView实现。您可能需要根据自己的需要进行自定义,但可能是一个良好的开端。只需通过调用initWithFrame创建它:并将您的UIView对象添加到children数组。你可能需要在旋转后调用setNeedsLayout,不记得了。

//
//  GridView.h
//  Project
//
//  Created by Anthony Picciano on 7/28/10.
//

#import <UIKit/UIKit.h>

#define GRID_VIEW_DEFAULT_COLUMNS 1
#define GRID_VIEW_DEFAULT_HGAP 10.0f
#define GRID_VIEW_DEFAULT_VGAP 10.0f
#define GRID_VIEW_LAYOUT_NOTIFICATION @"layoutNotification"


@interface GridView : UIView {
    int columns;
    float hgap;
    float vgap;
}

@property (nonatomic) int columns;
@property (nonatomic) float hgap;
@property (nonatomic) float vgap;

@end


//
//  GridView.m
//  Project
//
//  Created by Anthony Picciano on 7/28/10.
//

#import "GridView.h"


@implementation GridView
@synthesize columns, hgap, vgap;


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        columns = GRID_VIEW_DEFAULT_COLUMNS;
        hgap = GRID_VIEW_DEFAULT_HGAP;
        vgap = GRID_VIEW_DEFAULT_VGAP;
    }
    return self;
}

- (void)layoutSubviews {
    float xpos = 0;
    float ypos = 0;
    float width = self.frame.size.width;
    float cellWidth = (width - (hgap * (columns - 1))) / columns;

    int currentColumn = 1; // columns number start at 1, not 0
    float maxRowHeight = 0.0f;

    for (UIView *child in self.subviews) {
        CGRect childFrame = CGRectMake(xpos, ypos, cellWidth, child.frame.size.height);
        child.frame = childFrame;

        if (child.frame.size.height > maxRowHeight) {
            maxRowHeight = child.frame.size.height;
        }

        if (currentColumn < columns) {
            currentColumn++;
            xpos += cellWidth + hgap;
        } else {
            currentColumn = 1;
            xpos = 0.0f;
            ypos += maxRowHeight + vgap;
            maxRowHeight = 0.0f;
        }
    }

    if (currentColumn == 1) {
        ypos -= vgap;
    } else {
        ypos += maxRowHeight;
    }

    CGRect aFrame = self.frame;
    aFrame.size = CGSizeMake(width, ypos);
    self.frame = aFrame;    

    NSNotification *notification = [NSNotification notificationWithName:GRID_VIEW_LAYOUT_NOTIFICATION object:nil];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
}

- (void)dealloc {
    [super dealloc];
}


@end