xx.h文件數組
// // ListViewController.h // Wealthy Chat // // Created by cafuc on 16/4/10. // Copyright © 2016年 cafuc. All rights reserved. // #import <UIKit/UIKit.h> #import "NavigationBarView.h" @interface ListViewController : UIViewController<NavigationBarViewDelegate,UITableViewDelegate,UITableViewDataSource> { //tableView部分 UITableView *tableViewData; //數據數組 NSMutableArray *arrayData; } @end
xx.m文件code
// // ListViewController.m // Wealthy Chat // // Created by cafuc on 16/4/10. // Copyright © 2016年 cafuc. All rights reserved. // #import "ListViewController.h" #import "Utils.h" #import "ListCell.h" #import "CellData.h" @implementation ListViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //假數據 CellData *cellData = [[CellData alloc] init]; cellData.viewHead = @"https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=1445316376,1021770337&fm=58"; cellData.nickName = @"張三"; //cellData.gender = @"0"; cellData.age = @"29"; CellData *cellData1 = [[CellData alloc] init]; cellData1.viewHead = @"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2053660193,2970683644&fm=58"; cellData1.nickName = @"李四"; //cellData1.gender = @"1"; cellData1.age = @"30"; CellData *cellData2 = [[CellData alloc] init]; cellData2.viewHead = @"https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1892490377,1795030659&fm=58"; cellData2.nickName = @"王五"; //cellData2.gender = @"1"; cellData2.age = @"31"; //添加數據 arrayData = [[NSMutableArray alloc] init]; [arrayData addObject:cellData]; [arrayData addObject:cellData1]; [arrayData addObject:cellData2]; //初始化tableView tableViewData = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)]; tableViewData.delegate = self; tableViewData.dataSource = self; [self.view addSubview:tableViewData]; } //顯示多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSLog(@"個數%d",arrayData.count); return arrayData.count; } //行高 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 140; } //裝值 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"cell"; //初始化cell,並指定其類型 ListCell *cell = (ListCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { //建立cell cell = [[ListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } //取消點擊cell時顯示的背景色 cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell initCellData:[arrayData objectAtIndex:indexPath.row]]; //返回cell return cell; } //選中哪一行 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"選中第%d行",indexPath.row); } @end