AJ分享,必須精品
### 先看效果圖




### 代碼 ViewController
~~~
#import "NYViewController.h"
#import "NYCarGroup.h"
#import "NYCar.h"
@interface NYViewController () <UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation NYViewController
-(UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//設置數據源
_tableView.dataSource = self;
//加載上去
[self.view addSubview:_tableView];
}
return _tableView;
}
//懶加載
-(NSArray *)carGroups
{
if (_carGroups == nil) {
_carGroups = [NYCarGroup carGroups];
}
return _carGroups;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// NSLog(@"%@", self.carGroups);
// 調用tableView添加到視圖
[self tableView];
}
#pragma mark - tableView 數據源方法
/**分組總數*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carGroups.count;
}
/**每一組多少行 ,section是第幾組*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NYCarGroup * group = self.carGroups[section];
return group.cars.count;
}
/**單元格*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//可重用表示符
static NSString *ID = @"cell";
//讓表格去緩沖區查找可重用cell
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
//如果沒有找到可重用cell
if (cell == nil) {
//實例化cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//設置cell 內容
//取出數據模型
NYCarGroup *group = self.carGroups[indexPath.section];
NYCar *car = group.cars[indexPath.row];
//設置數據
cell.imageView.image = [UIImage imageNamed:car.icon];
cell.textLabel.text = car.name;
return cell;
}
/**每一組的標題*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.carGroups[section] title];
}
/** 右側索引列表*/
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
/*
索引數組中的"內容",跟分組無關
索引數組中的下標,對應的是分組的下標
return @[@"哇哈哈", @"hello", @"哇哈哈", @"hello", @"哇哈哈", @"hello", @"哇哈哈", @"hello"];
返回self.carGroup中title的數組
NSMutableArray *arrayM = [NSMutableArray array];
for (HMCarGroup *group in self.carGroups) {
[arrayM addObject:group.title];
}
return arrayM;
KVC是cocoa的大招
用來間接獲取或者修改對象屬性的方式
使用KVC在獲取數值時,如果指定對象不包含keyPath的"鍵名",會自動進入對象的內部查找
如果取值的對象是一個數組,同樣返回一個數組
*/
/*例如:
NSArray *array = [self.carGroups valueForKeyPath:@"cars.name"];
NSLog(@"%@", array);
*/
return [self.carGroups valueForKeyPath:@"title"];
}
@end
~~~
### 模型代碼
NYCar.h
~~~
//
// NYCar.h
// 06-汽車品牌帶右側索引
#import <Foundation/Foundation.h>
@interface NYCar : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
-(instancetype) initWithDict:(NSDictionary *)dict;
+(instancetype) carWithDict:(NSDictionary *)dict;
// 傳入一個包含字典的數組,返回一個HMCar模型的數組
+(NSArray *) carsWithArray:(NSArray *)array;
@end
~~~
NYCar.m
~~~
//
// NYCar.m
// 06-汽車品牌帶右側索引
//
// Created by apple on 15-3-29.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYCar.h"
@implementation NYCar
-(instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
+(NSArray *)carsWithArray:(NSArray *)array
{
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
[arrayM addObject:[self carWithDict:dict]];
}
return arrayM;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p> {name: %@, icon: %@}",self.class, self, self.name, self.icon ];
}
@end
~~~
NYCarGroup.h
~~~
//
// NYCarGroup.h
// 06-汽車品牌帶右側索引
#import <Foundation/Foundation.h>
@interface NYCarGroup : NSObject
/** 首字母 */
@property (nonatomic, copy) NSString *title;
/** 車的數組,存放的是HMCar的模型數據 */
@property (nonatomic, strong) NSArray *cars;
-(instancetype) initWithDict:(NSDictionary *)dict;
+(instancetype) carGroupWithDict:(NSDictionary *)dict;
+(NSArray *) carGroups;
@end
~~~
NYCarGroup.m
~~~
//
// NYCarGroup.m
// 06-汽車品牌帶右側索引
//
#import "NYCarGroup.h"
#import "NYCar.h"
@implementation NYCarGroup
-(instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
// [self setValuesForKeysWithDictionary:dict];
// dict[@"cars"]存放的是字典的數組
// 希望將字典的數組轉換成HMCar模型的數組
// [self setValue:dict[@"cars"] forKey:@"cars"];
[self setValue:dict[@"title"] forKeyPath:@"title"];
self.cars = [NYCar carsWithArray:dict[@"cars"]];
}
return self;
}
+(instancetype)carGroupWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
+(NSArray *)carGroups
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in array) {
[arrayM addObject:[self carGroupWithDict:dict]];
}
return arrayM;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p> {title: %@, cars: %@}", self.class, self, self.title, self.cars];
}
@end
~~~
代碼偶了
### 注意點
實現右側索引:
~~~
/** 右側索引列表*/
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
~~~
對dataSource復習
~~~
@required的兩個
/**單元格*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
/**每一組多少行 ,section是第幾組*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
~~~
~~~
/**分組總數*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
/**每一組的標題*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
~~~
### 出錯誤了,求助失敗[apple mach - o linker error]
聽說學iOS百度沒用。。。今天真信了,出了個錯誤,
【apple mach - o linker error】
如圖

{
常見錯誤描述:
Apple Mach-O Linker Error這類錯誤的錯誤信息最后一行通常如下:
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang failed with exit code 1
發生這種錯誤的原因通常是因為項目中存在同名類造成鏈接錯誤。
有可能是你存在兩個類名稱都一樣,也可能是因為你在不同的.m文件中定義了同樣的const變量。
這類錯誤需要自己看錯誤信息中給出的大長串路徑,從中找出你的那個重名類或者變量名稱,以此來定位錯誤位置。
}
好了 我只想說,AJ自己把代碼拷貝出來,然后又關了xcode ,最后重新建立了一個項目,把代碼不動得放回,就這么華麗麗的好了。。。。。。。。。。。。
?
?
?