1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
#import "MyTableViewController.h"
@interface MyTableViewController ()
//定义两个数组,分别存储 城市和人口信息
@property(nonatomic,strong) NSArray *allCitys;
@property(nonatomic,strong) NSArray *allPopulation;
@end
@implementation MyTableViewController
//推荐懒加载 延迟加载--在第一次使用 的时候,才创建实例
-(NSArray *)allCitys{
if (_allCitys == nil) {//!_allCitys or nil == _allCitys 皆可以
_allCitys = @[@"北京",@"上海",@"广州",@"深圳",@"南京"];
}
return _allCitys;
}
-(NSArray *)allPopulation{
/*
NSNumber *num = @100;//也是一种封装方式
*/
if (!_allPopulation) {
_allPopulation = @[@3000,@2000,@2500,@2048,@1500];//??
}
return _allPopulation;
}
//
// if (!_allPopulation) {
// _allPopulation = @[@"3000",@"2000",@"2500",@"2048",@"1500"];
// }
// return _allPopulation;
//}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" /*forIndexPath:indexPath*/];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
}
// 第几行显示第几个元素
//
cell.textLabel.text = self.allCitys[indexPath.row];
//
// cell.detailTextLabel.text = self.allPopulation[indexPath.row];
//
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@万",self.allPopulation[indexPath.row]];
//
cell.imageView.image = [UIImage imageNamed:@"head"];
return cell;
}
#pragma mark - Table view delegate
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 获取点中的 cell 实例
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];//???
NSLog(@"%@的人口是%@",cell.textLabel.text,cell.detailTextLabel.text);
}
@end
|