天天看点

带有Secitond的Cell

//

//  ViewController.h

//  kkk

//

//  Created by 周晓雯 on 15/8/24.

//  Copyright (c) 2015年 周晓雯. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

{

    UITableView *listTableView;

    NSArray *sectionTitleArray;

}

@end

粘贴到项目中运行加注释就可以清楚地知道作用了

//

//  ViewController.m

//  kkk

//

//  Created by 周晓雯 on 15/8/24.

//  Copyright (c) 2015年 周晓雯. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    sectionTitleArray = [NSArray arrayWithObjects:@"1-10",@"11-20",@"21-30",@"31-40",@"41-50",@"51-60",@"61-70",@"71-80",@"81-90",@"91-100", nil];

    UITableView *tv = [[UITableView alloc] initWithFrame:self.view.bounds];

    tv.dataSource = self;

    tv.delegate = self;

    listTableView = tv;

    [self.view addSubview:tv];

//    UIView *hview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320.f, 200.f)];

//    

//    hview.backgroundColor = [UIColor orangeColor];

//    listTableView.tableHeaderView = hview;

}

//右边索引 字节数(如果不实现 就不显示右侧索引)

//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

//    

//    

//    

//    return sectionTitleArray;

//    

//}

//section (标签)标题显示

//

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    return [sectionTitleArray objectAtIndex:section];

}

//Sections标签数

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [sectionTitleArray count];

}

// 设置中间section的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section == 1) {

        return 80;

    }

    return 20;

}

//点击右侧索引表项时调用

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

    NSString *key = [sectionTitleArray objectAtIndex:index];

    NSLog(@"sectionForSectionIndexTitle key=%@",key);

    if (key == UITableViewIndexSearch) {

        [listTableView setContentOffset:CGPointZero animated:NO];

        return NSNotFound;

    }

    return index;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *v = nil;

    if (section == 0) {

        v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];

        [v setBackgroundColor:[UIColor grayColor]];

        UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 10.0f, 200.0f, 30.0f)];

        [labelTitle setBackgroundColor:[UIColor clearColor]];

        labelTitle.textAlignment = NSTextAlignmentCenter;

        labelTitle.text = @"第一个section 定制页面";

        [v addSubview:labelTitle];

    }

    return v;

}

// 设置cell的高度

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 44;

}

//每个Section:显示的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *detailIndicated = @"tableCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:detailIndicated];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:detailIndicated];

        cell.tag = indexPath.row;

    }

    cell.textLabel.text = [NSString stringWithFormat:@"%lu",10*indexPath.section + indexPath.row + 1];

    return cell;

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end