swift学习,tableView的不同写法,使用懒加载方式
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 import UIKitlet kScreenHeight = UIScreen .mainScreen().bounds.size.heightlet kScreenWidth = UIScreen .mainScreen().bounds.size.widthlet kCellIdentifier = "CellIdentifier" class ViewController : UIViewController { override func viewDidLoad () { super .viewDidLoad() view.addSubview(tableView); view.addSubview(tableView1); } lazy var tableView : UITableView = { let temptableView = UITableView (frame: CGRectMake (0 , 0 ,kScreenWidth, kScreenHeight/ 2 - 10 ), style: UITableViewStyle .Grouped ) temptableView.delegate = self temptableView.dataSource = self return temptableView }() lazy var tableView1: UITableView ! = { var tableView = UITableView (frame: CGRectMake (0 ,kScreenHeight/2,kScreenWidth, kScreenHeight/ 2 - 10 ), style: UITableViewStyle .Grouped ) tableView.delegate = self tableView.dataSource = self tableView.registerClass(UITableViewCell .self , forCellReuseIdentifier:kCellIdentifier) return tableView }() lazy var dataList:[String ] = { return ["iOS001" ,"iOS002" ,"iOS003" ,"iOS007" ]; }() } extension ViewController : UITableViewDelegate ,UITableViewDataSource { func tableView (tableView : UITableView , numberOfRowsInSection section : Int ) -> Int { return dataList.count } func tableView (tableView : UITableView , cellForRowAtIndexPath indexPath : NSIndexPath ) -> UITableViewCell { if tableView == self .tableView { let identifier = "CellIdentifier" var cell = tableView.dequeueReusableCellWithIdentifier(identifier) if cell == nil { cell = UITableViewCell (style: .Default , reuseIdentifier: identifier) } cell? .textLabel? .text = "tableView*显示行数\(indexPath.row) -\(dataList[indexPath.row]) " return cell! ; }else { let cell2 = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier, forIndexPath: indexPath) cell2.textLabel? .text = "tableView1*显示行数-\(dataList[indexPath.row]) " return cell2; } } }
OC写法
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 #import "ViewController.h" @interface ViewController () <UITableViewDelegate , UITableViewDataSource >@property (nonatomic , strong ) UITableView *tableView;@property (nonatomic , strong ) NSArray *dataSource;@end @implementation ViewController #pragma mark - life cycle - (void )viewDidLoad { [super viewDidLoad]; [self .view addSubview:self .tableView]; } #pragma mark - UITableViewDataSource - (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger )section { return self .dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; cell.textLabel.text = self .dataSource[indexPath.row]; return cell; } #pragma mark - UITableViewDelegate - (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES ]; } #pragma mark - getters and setters NSString * const kCellIdentifier = @"kCellIdentifier" ;- (UITableView *)tableView { if (_tableView == nil ) { _tableView = [[UITableView alloc] initWithFrame:CGRectMake (0 ,20 , self .view.bounds.size.width, self .view.bounds.size.height) style:UITableViewStylePlain ]; _tableView.delegate = self ; _tableView.dataSource = self ; [_tableView registerClass:[UITableViewCell class ] forCellReuseIdentifier:kCellIdentifier]; } return _tableView; } - (NSArray *)dataSource { if (_dataSource == nil ) { _dataSource = @[@"First controller" , @"Second controller" , @"Third controller" ]; } return _dataSource; } @end