////? ViewController.m
//? D1-SingleGroupTableView//
//? Created by? CE? on 16/4/15.
//? Copyright ? 2016年 CE. All rights reserved.
//#import "ViewController.h"@interface ViewController ()//數(shù)據(jù)源
@property NSMutableArray *dataSource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建數(shù)據(jù)
[self createDataSource];
//創(chuàng)建表格視圖
[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號(hào)", i+1];
[self.dataSource addObject:str];
}
}
- (void)createTableView {
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
//設(shè)置數(shù)據(jù)源代理
tableView.dataSource = self;
//注冊(cè)cell類型以及復(fù)用標(biāo)識(shí)
[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 {
//面試
//表格視圖內(nèi)部維護(hù)了一個(gè)cell的復(fù)用隊(duì)列贡耽,每次需要新的cell時(shí),可以先從隊(duì)列中根據(jù)復(fù)用標(biāo)識(shí)尋找是否有空閑的cell涧衙,若有則直接出列使用無(wú)需創(chuàng)建新的;若沒(méi)有可用cell則需要?jiǎng)?chuàng)建新的菇用。
//表格視圖上的cell離開顯示區(qū)域就會(huì)自動(dòng)放入復(fù)用隊(duì)列
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
#if 0
//若不想判斷塞弊,則前面需要注冊(cè)cell類型及同復(fù)用標(biāo)識(shí)
if (!cell) {
static int count = 0;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
count++;
printf("創(chuàng)建cell%d\n", count);
}
#endif
cell.textLabel.text=self.dataSource[indexPath.row];
return cell;
}
@end