1 1、UICollectionView和UICollectionViewController的用法 2 1.新建一個xib描述UICollectionViewCell(好比DealCell.xib),設置好resuse identifier(好比deal) 3 4 2.控制器繼承UICollectionViewController 5 1> 註冊xib 6 [self.collectionView registerNib:[UINib nibWithNibName:@"DealCell" bundle:nil] forCellWithReuseIdentifier:@"deal"]; 7 8 2> 重寫init方法 9 - (id)init 10 { 11 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; 12 // 每個網格的尺寸 13 layout.itemSize = CGSizeMake(250, 250); 14 // 每一行之間的間距 15 layout.minimumLineSpacing = 20; 16 // 上下左右的間距 17 layout.sectionInset = UIEdgeInsetsMake(10, 20, 40, 80); return [self initWithCollectionViewLayout:layout]; 18 } 19 20 3> 實現數據源方法 21 #pragma mark 每一組有多少個條目(格子) 22 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 23 #pragma mark 每個格子顯示什麼樣的cell 24 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 25 26 3.UICollectionViewFlowLayout的常見設置 27 1> CGFloat minimumLineSpacing:每一行之間的間距 28 2> UIEdgeInsets sectionInset:上下左右周邊的間距 29 3> CGSize itemSize:每個網格的大小 30 31 4.UICollectionView的設置 32 1> BOOL alwaysBounceVertical:永遠支持垂直的彈簧效果(滾動效果,來自UIScrollView的屬性) 33 34 5.UITableViewController和UICollectionViewController的區別 35 1> 在UITableViewController中:self.tableView == self.view 36 2> 在UICollectionViewController中:self.collectionView == self.view中的一個子控件 37 38 2、iPad中控制器view初始的width和height 39 1> 規律 40 * width 是寬高中最小的那個值 41 * height 是寬高中最大的那個值 42 43 2> 舉例(好比窗口根控制器的view,有狀態欄的狀況下) 44 * 橫屏 width = 748,height = 1024 45 * 豎屏 width = 768,height = 1004 46 47 3、控制器的重要方法使用 48 1.屏幕即將旋轉的時候調用(控制器監控屏幕旋轉) 49 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 50 51 2.屏幕旋轉完畢的時候調用 52 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 53 54 3.view建立完畢的時候調用 55 - (void)viewDidLoad 56 這個方法調用時,控制器的view只是一個初始寬高(如二中所說的width和height) 57 58 4.view即將顯示的時候調用 59 - (void)viewWillAppear:(BOOL)animated 60 這個方法調用時,控制器view才能獲得最真實的寬高 61 62 5.view顯示完畢的時候調用 63 - (void)viewDidAppear:(BOOL)animated 64 這個方法調用時,控制器view已經能獲得最真實的寬高