自定義cell
阿新 • • 發佈:2018-11-16
第一步:
AppDelegate.m
首先匯入標頭檔案 #import “ViewController.h”
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];
第二步:
ViewController.m
<UITableViewDelegate,UITableViewDataSource>
接下來viewDidLoad裡
// 導航條 50 111 171 self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:50/256.0 green:111/256.0 blue:171/256.0 alpha:1.0]; UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 150, 40)]; imgV.image = [UIImage imageNamed:@"1"]; self.navigationItem.titleView =imgV; // 右邊按鈕 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"2"] style:UIBarButtonItemStyleDone target:self action:nil]; // 表格 // 1.frame UITableView *tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain]; // 2.資料來源代理 tbv.delegate = self; tbv.dataSource = self; // 行高 tbv.rowHeight = 100; // 3.新增 [self.view addSubview:tbv]; } // 幾行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 3; } // 每行有啥 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *str = @"11"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if(cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]; } // 圖片 NSArray *imgArr = @[@"3",@"4",@"5"]; UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 120, 80)]; // imgV.backgroundColor = [UIColor redColor]; imgV.image = [UIImage imageNamed:imgArr[indexPath.row]]; [cell addSubview:imgV]; // 主標題 NSArray *zArr = @[@"科目三電子考要來了",@"駕考新規要來了,你準備好了嗎",@"小夥伴,要不要體驗一下職能教學"]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 12, 350, 30)]; label.text = zArr[indexPath.row]; label.font = [UIFont systemFontOfSize:15]; [cell addSubview:label]; // 按鈕 UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(150, 60, 60, 26)]; [btn setTitle:@"駕校公告" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:13]; btn.layer.borderColor = [UIColor orangeColor].CGColor; btn.layer.borderWidth = 1; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 4; [cell addSubview:btn]; // 主標題 NSArray *Arr = @[@"8天前",@"1分前",@"2小時前"]; UILabel *tlabel = [[UILabel alloc]initWithFrame:CGRectMake(350 , 60, 60, 25)]; tlabel.text = Arr[indexPath.row]; tlabel.font = [UIFont systemFontOfSize:15]; tlabel.textAlignment = NSTextAlignmentCenter; tlabel.textColor = [UIColor lightGrayColor]; [cell addSubview:tlabel]; return cell; }
執行效果: