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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| #import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UITableView *_tableView; } @end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; [self createUITableView]; } -(void)createUITableView{
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor orangeColor];
_tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView];
UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]; headerImageView.image = [UIImage imageNamed:@"1.jpg"]; _tableView.tableHeaderView = headerImageView;
UIImageView *footerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]; footerImageView.image = [UIImage imageNamed:@"2.jpg"]; _tableView.tableFooterView = footerImageView; } #pragma mark - #pragma mark dataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 2; }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return 10; } return 15; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } cell.textLabel.text = [NSString stringWithFormat:@"第%ld组-第%ld行",indexPath.section,indexPath.row]; return cell; } #pragma mark - #pragma mark delegate
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ if (section == 0) { return @"第一组头标题"; } return @"第二组头标题"; }
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40; }
-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ if (section == 0) { return @"第一组脚标题"; } return @"第二组脚标题"; }
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 40; }
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0, 0, self.view.frame.size.width, 40); [button setTitle:@"分组标题" forState:UIControlStateNormal]; button.backgroundColor = [UIColor purpleColor]; [button addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside]; return button; } -(void)onClick{ NSLog(@"点点点点点"); }
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)]; imageView.image = [UIImage imageNamed:@"6.jpg"]; return imageView; } @end
|