0
我明白要創建自定義單元才能實現該目標。但是,youtube tableview會根據方向重新排列自己。如果考慮到必須將單元出列,那麼如何維護?YouTube喜歡iPad中的TableView,問題與定位
我明白要創建自定義單元才能實現該目標。但是,youtube tableview會根據方向重新排列自己。如果考慮到必須將單元出列,那麼如何維護?YouTube喜歡iPad中的TableView,問題與定位
這是一個非常簡單的GridView實現。您可能需要根據您的需求進行定製,但可能是一個好的開始。只需通過調用initWithFrame來創建它:然後將你的UIView對象添加到子數組中。旋轉後您可能需要調用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