UITableView及簡單通訊錄功能

import "AppDelegate.h"

import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    RootViewController *rootVC = [[RootViewController alloc]init];
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:rootVC];
    [self.window setRootViewController:navC];

    return YES;
    }


import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)NSArray *dataArray; // 數據源,用來給cell賦值
@property (nonatomic ,retain)NSDictionary *dict;
@property (nonatomic ,retain)NSMutableArray *titleArray; // 用來盛放頭標題

@end

@implementation RootViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化數據源并且添加數據
    self.dataArray = [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J", nil];

    NSArray *arr = [NSArray arrayWithObjects:@"G",@"E",@"F",@"G",@"H", nil];
    self.dict = [NSDictionary dictionaryWithObjectsAndKeys:_dataArray,@"0",arr,@"1", nil];

    // 添加頭標題
    self.titleArray = [NSMutableArray array];
    for (NSArray *arrayItem in self.dict.allValues) {
    // 從數組中取出第一個元素
    // 判斷字典中的元素是否存在,如果存在它的類型是否為數組 且存在數組元素(不為空)
    if (arrayItem && [arrayItem isKindOfClass:[NSArray class]] && arrayItem.count) {
    NSString *nameStr = [arrayItem objectAtIndex:0];
    // 判斷數組中的元素是否存在,如果存在類型是否為字符串,如果為字符串類型,判斷字符串長度是否為零
    if (nameStr && [nameStr isKindOfClass:[NSString class]] && nameStr.length) {
    // 截取字符串的首個字符
    NSString *resultStr = [nameStr substringToIndex:1];
    // 將首個字符串添加進數組
    [self.titleArray addObject:resultStr];
    }
    }
    }

    self.navigationItem.title = @"RootVC";
    // 創建表視圖
    // 設置分割線樣式 style:設置單元格樣式,有兩種樣式:plain(平鋪) group ,默認樣式為:plain
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    // 設置分割線樣式
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    // 設置分割線顏色
    tableView.separatorColor = [UIColor orangeColor];
    // 如果我們每個的cell的高度是統一的,可以直接用屬性來設置
    tableView.rowHeight = 60;

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

pragma mark --- 表視圖的代理方法

// 共有多少個分區 此代理方法為可選的,如果不實現該代理方法,默認整個表視圖只有一個分區
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// return 1;
// 返回字典中元素的個數,作為分區的個數
return self.dict.count;
}

// 每個分區下返回的行數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// return 10;
// 這里單元格個數一般不寫死,將數據源的個數作為返回值,根據數據的數量創建單元格的數量
// return self.dataArray.count;// 返回所有數據的個數
// 根據當前所在的分區,取得字典中對應的鍵
// NSString *keyString = self.dict.allKeys[section];
// // 根據鍵取出對應的值,由于字典的值為數組,所以可以有count屬性
// return [self.dict[keyString] count];

//  根據所在分區取出字典的值
NSArray *array = [self.dict.allValues objectAtIndex:section];
return array.count;

}

// 定義單元格 IndexPath:單元格當前所在位置
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"cell";
//  identifier: 因為一個表視圖中可能存在多種樣式的單元格,我們把相同樣式的單元格放到同一個集合里面,為這個集合加標示符,當我們需要用到某種樣式的單元格的時候,根據不同的標示符,從不同的集合中找尋單元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//  在某個標示符下,可以再度被移出重新使用的cell

// ? 如果從集合中為找到單元格,也就是集合中好沒有單元格,也就是還沒有單元格出屏幕,那么我們需要創建單元格
if (!cell) {
// 創建cell的時候需要標示符是因為,當該cell出屏幕的時候將單元格按照不同類型放入和集中
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    //  設置cell的Style,不涉及到數據的重新賦值,我們可以在初始化cell的時候給它設置好
    //  設置輔助視圖
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    //  設置選中后的效果
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}

//  創建單元格

// UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

//  為cell上添加文字      indexPath.row : 單元格所在的行數

// cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld個單元格,我在%ld分區",(long)indexPath.row+1,indexPath.section];
// indexPath.row是得到當前單元格所在的那一行,起始位置為0,數組下標的起始位置也是零,所以我們可以根據單元格所在的行數來從數組中取值顯示
// cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];

// NSString *keyString = self.dict.allKeys[indexPath.section];
// NSArray *array = self.dict[keyString];
NSArray *valueArray = [self.dict.allValues objectAtIndex:indexPath.section];

cell.textLabel.text = [valueArray objectAtIndex:indexPath.row];

//  副標題  這種樣式只能在非default的樣式下使用,只為顯示位置有所改變
cell.detailTextLabel.text = @"副標題,顏色淺色";
//  不管任何Style樣式下,都給可以給cell添加圖片
cell.imageView.image = [UIImage imageNamed:@"11"];

return cell;
}

// 為每個分區頭添加標題的代理方法
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// 為每個分區添加頭標題
// NSArray *titArray = [NSArray arrayWithObjects:@"1",@"2", nil];
// return [titArray objectAtIndex:section];

// NSString *string = [[self.dict.allValues objectAtIndex:section] objectAtIndex:0];
// NSString *subStr = [string substringToIndex:1];
// return subStr;

int index = (int)(self.titleArray.count > section ? section : -1);
if (index != -1) {
    return [self.titleArray objectAtIndex:section];
}else
    return @"數組元素不夠了";

}

//? 點擊cell所響應的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 根據indexPath得到當前所點擊的cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"------%ld",(long)indexPath.row);
}

// 通過代理設置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//  一般cell都需要根據內容來自適應高度,高度的變化就在此處根據indexPath來更改(每個cell的高度不統一)
//  設置第一個cell高度200,其他都是100
if (indexPath.row == 0) {
    return 100;
}
return 60;

}

// 添加右側索引條的代理方法
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    @end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容