////? ViewController.m
//? D1-SingleGroupTableView//
//? Created by? CE? on 16/4/15.
//? Copyright ? 2016年 CE. All rights reserved.
//#import "ViewController.h"@interface ViewController ()//數據源
@property NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//創建數據
[self createDataSource];
//創建表格視圖
[self createTableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)createDataSource {
self.dataSource = [NSMutableArray array];
for (NSUInteger i=0; i<50; i++) {
NSString *str = [NSString stringWithFormat:@"科密%.2lu號", i+1];
[self.dataSource addObject:str];
}
}
- (void)createTableView {
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
//設置數據源代理
tableView.dataSource = self;
//注冊cell類型以及復用標識
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];
[self.view addSubview:tableView];
}
#pragma mark - UITableViewDataSource
//有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataSource.count;
}
//哪一組的哪一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//面試
//表格視圖內部維護了一個cell的復用隊列,每次需要新的cell時,可以先從隊列中根據復用標識尋找是否有空閑的cell,若有則直接出列使用無需創建新的;若沒有可用cell則需要創建新的。
//表格視圖上的cell離開顯示區域就會自動放入復用隊列
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
#if 0
//若不想判斷,則前面需要注冊cell類型及同復用標識
if (!cell) {
static int count = 0;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
count++;
printf("創建cell%d\n", count);
}
#endif
cell.textLabel.text=self.dataSource[indexPath.row];
return cell;
}
@end