表視圖1 (UITabelView)

初始化

這裏的UITableViewStyle有兩種格式 一種是UITableViewStylePlain
還有一種是分組格式
UITableViewStyleGrouped

UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];

設置代理 和 數據源

在設置代理之前 我們要先簽兩個協議

tableView.delegate = self;
tableView.dataSource = self;

設置整個tableView的表頭和表尾

只有高度Height可以改變

UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
    headerView.backgroundColor = [UIColor cyanColor];
    tableView.tableHeaderView = headerView;
    [headerView release];

x軸與高度height都可以改變表尾在整個視圖中的位置

UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(100, 40, 40, 90)];
    footerView.backgroundColor = [UIColor orangeColor];
    tableView.tableFooterView = footerView;
    [footerView release];

dataSource 必須要實現的兩個方法

返回分區數

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

返回每個分區有多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

在表視圖中 還有一個必要的方法 就是返回單元格方法

在給單元格cell賦值時 要考慮到給你的是什麼數據類型,你再慢慢抽絲剝繭 就像剝洋蔥一樣 最後賦值到cell上

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];

    }
    //賦值的位置
    NSArray *keys = self.dataDic.allKeys;
    NSString *key = keys[indexPath.section];
    NSArray *values = self.dataDic[key];
    //用model來接收數組中的model
    CellModel *model = values[indexPath.row];


    //賦值cell
    cell.textLabel.text = model.title;
    cell.imageView.image = [UIImage imageNamed:model.imageName];

    return cell;

}
 //設置單元格cell的標題
    cell.textLabel.text = @"哈哈";
    cell.detailTextLabel.text = @"呵呵";
    //設置cell上的圖片
    cell.imageView.image = [UIImage imageNamed:@"00.jpg"];
    //設置輔助按鈕
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

設置每個分區的表頭 和 表尾

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
    headerView.backgroundColor = [UIColor redColor];


    return [headerView autorelease];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 150)];
    footerView.backgroundColor = [UIColor greenColor];

    return [footerView autorelease];
}

設置分區表頭 和 表尾 的高度

在這裏 一般設置分區表頭 和 表尾 的高度 都要和 設置每個分區的表頭 和 表尾 一起書寫

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50;
}

返回的是每一分區的每一行 的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

設置分區表頭 的 標題

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //NSLog(@"%ld",section);
    //return @"w";
    if (section == 0) {
        return @"a";
    }else if(section == 1)
    {
        return @"b";
    }else
    {
        return @"c";
    }
}

這裏寫圖片描述
左邊是分區表頭 的 標題 右邊是標題小按鈕
對號是設置輔助按鈕
cell.accessoryType = UITableViewCellAccessoryCheckmark;

設置tabelView右邊 標題小按鈕

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"a",@"b",@"c"];
}

這裏加一個完整的代碼
這是一個RootViewController.m的代碼
這裏面少了一張圖片

//
//  RootViewController.m
//  09-UITableView-02
//
//  Created by lanou on 15/11/20.
//  Copyright (c) 2015年 yht. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
//保存數據的數組
@property (nonatomic ,retain)NSArray *dataArray;

//保存數據的字典
@property (nonatomic ,retain)NSDictionary *dataDic;
//保存完成排序的所有key
@property (nonatomic ,retain)NSArray *sortKeysArray;
@end

@implementation RootViewController

-(void)dealloc
{
    [_dataArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addTableView];
    [self setUpData];

}

//// 加載數據的方法
//- (void)setUpDate
//{
//    
//    self.dataArray = @[@"我欲封天",@"大主宰",@"靈域",@"武煉巔峯",@"詹姆斯",@"科比",@"哈登",@"韋德",@"保羅"];
//}
//  處理數據
- (void)setUpData
{
    NSArray *groupD = [NSArray arrayWithObjects:
                       @"徐陽",
                       @"趙姐", nil];
    NSArray *groupC = [NSArray arrayWithObjects:
                       @"張文",
                       @"裝裝用", nil];
    NSArray *groupM = [NSArray arrayWithObjects:
                       @"張傑",
                       @"尼鵬",
                       @"汪峯", nil];
    NSArray *groupN = [NSArray arrayWithObjects:
                       @"張豐田",
                       @"羽凡",
                       @"馬澤共",
                       @"吳玉成",
                       @"宋玉峯", nil];
    NSArray *groupE = [NSArray arrayWithObjects:
                       @"消費",
                       @"相遇",
                       @"明洋",
                       @"剛哥",
                       @"大君",
                       @"子龍",nil];
    NSArray *groupK= [NSArray arrayWithObjects:
                      @"聽聽",
                      @"榮飛",nil];
    NSArray *groupA = [NSArray arrayWithObjects:
                       @"精超",nil];
    NSArray *groupZ = [NSArray arrayWithObjects:
                       @"棄用",
                       @"之言",
                       @"兵器",
                       @"鵬舉",
                       @"江龍",
                       @"遊街", nil];

    self.dataDic = @{@"A":groupA, @"Z":groupZ, @"C":groupC, @"D":groupD, @"K":groupK, @"E":groupE, @"M":groupM, @"N":groupN};

    //  取出所有Key
    NSArray *keys = [self.dataDic allKeys];
    //  對key進行排序
    self.sortKeysArray = [keys sortedArrayUsingSelector:@selector(compare:)];
}



//初始化UITableView
- (void)addTableView
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];

    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView release];

}

//返回每個分區多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //數組的元素的個數
    //return self.dataArray.count;
    //return self.dataDic[self.sortKeysArray[section]].count;

    //通過這個字典  先找到分區對應的key
    //用這個key取出對應的數組
    //返回 這個數組的count
    NSString *key = self.sortKeysArray[section];
    NSArray *values = self.dataDic[key];
    return values.count;


}

//返回分區數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //有多少鍵值對  就要多少分區
    return self.sortKeysArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     UITableViewCell重用
     需要一個重用的集合  作用:把滑出屏幕的cell(完全消失在屏幕上時)  放入這個重用集合(備用)
     當屏幕下方需要新的cell進行展示的時候 開始重用
     方式是  首先 系統會先去重用集合中找   看有沒有cell可以重新使用  如果有 就直接使用  如果沒有 
     就創建一個出來進行使用
     */

    static NSString *identifier = @"MyCell";
    //去重用集合中 按標識符  尋找對應的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        //去創建新的cell
        //釋放cell的時候  只有創建出來才需要去釋放  從集合中取出來  不要釋放了
        cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];
        static int num = 0;
        num++;
         NSLog(@"%d",num);
    }
    //賦值cell上的控件(從集合中取出來的   也需要賦值)

    //就一個分區 分區indexPath.section  始終是0
    //row 是每一個分區的第幾行
    //cell.textLabel.text = self.dataArray[indexPath.row];

    //字典的展示數據
    //用分區 找出對應的key
    //用key 找出對應的 數組value
    //用行 找出 每一行要顯示的字
    NSString *key = self.sortKeysArray[indexPath.section];
    NSArray *values = self.dataDic[key];
    NSString *name = values[indexPath.row];
    cell.textLabel.text = name;


    return cell ;





//    NSString *identifier = @"MyCell";
//    UITableViewCell *tableViewCell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
//    //tableViewCell.imageView.image = [UIImage imageNamed:@"01.jpg"];
//    tableViewCell.textLabel.text = @"一個字";
//    tableViewCell.detailTextLabel.text = @"haha";
//    
//    return [tableViewCell autorelease];

}

//設置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
//設置分區的標題
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.sortKeysArray[section];
}
//設置小按鈕
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sortKeysArray;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
發佈了32 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章