iOS--通訊錄(UITableViewController)

来源:http://www.cnblogs.com/bolin-123/archive/2016/03/18/5293162.html
-Advertisement-
Play Games

本文主要實現通訊錄的部分功能(分組名、索引、分組的組名)等等功能: 廢話不多說了,先上效果圖: 在工程中需要導入一個plist文件,文件圖如圖: 工程目錄文件如圖: 工程程式如圖所示: RootTableViewController.h RootTableViewController.m 註: 通訊


本文主要實現通訊錄的部分功能(分組名、索引、分組的組名)等等功能:

廢話不多說了,先上效果圖:

 

在工程中需要導入一個plist文件,文件圖如圖:

工程目錄文件如圖:

 

 

工程程式如圖所示:

RootTableViewController.h

 

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController
@property(strong,nonatomic) NSDictionary *dic;
@property(strong,nonatomic) NSArray *arrkeys;
@end

 

 

RootTableViewController.m

#import "RootTableViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    self.dic=[NSDictionary dictionaryWithContentsOfFile:path];
//    NSLog(@"%@",self.dic);
//    NSLog(@"%@",self.dic.allKeys);
    
    // 字典排序  字典的輸出是無序的, 需要排序
    self.arrkeys=[self.dic.allKeys sortedArrayUsingSelector:@selector(compare:)];
//    NSLog(@"%@",self.arrkeys);
    // 唯一標識符
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
}

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

#pragma mark - 每個組的數量

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arrkeys.count;
}

#pragma mark - 組數列表
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key=self.arrkeys[section];
    NSArray *tempArr=self.dic[key];
//    NSLog(@"%@",key);
    return tempArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    NSString *key=self.arrkeys[indexPath.section];
    NSArray *tempArr=self.dic[key];
    NSString *name=tempArr[indexPath.row];
    
    cell.textLabel.text=name;
    
    return cell;
}

#pragma  mark - 返回行高的方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark - 系統預設的開頭關鍵字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.arrkeys[section];
    
//    return [NSString stringWithFormat:@"%c",(char)('A'+section)];
    
}

#pragma mark - 自定義自體大小的開頭關鍵字
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lbl=[[UILabel alloc] init];
    
    lbl.backgroundColor=[UIColor redColor];
    lbl.tintColor=[UIColor yellowColor];
    lbl.text=self.arrkeys[section];
    lbl.font=[UIFont systemFontOfSize:30.0];
    return lbl;
}
#pragma mark - 設置右邊索引高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
}

#pragma mark - 設置所有分區的標題的列表
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.arrkeys;
}

#pragma mark -  單選控制格式
-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0) {
        return UITableViewCellAccessoryCheckmark;
    }
    else if(indexPath.row==1){
        return UITableViewCellAccessoryDetailDisclosureButton;
    }
    else if(indexPath.row==2){
        return UITableViewCellAccessoryDisclosureIndicator;
    }else{
        return UITableViewCellAccessoryNone;
    }
}

.........
@end

 

註:

通訊錄中的各種屬性和顯示規格都在RootTableViewController.m里設置!!!

 

AppDelegate.h

#import <UIKit/UIKit.h>
#import "RootTableViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

 

AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window.rootViewController=[[UINavigationController alloc] initWithRootViewController:[[RootTableViewController alloc] initWithStyle:UITableViewStyleGrouped]];
    
    return YES;
}
.......

@end

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文介紹了app歡迎頁的簡單實現。只有第一次運行程式時才說會出現,其餘時間不會出現。下麵是效果圖。 代碼如下:(如有不明白的可以評論我,我會詳細講解)
  • 繼承和類設計 oc是一門面向對象的語言,面向對象編程有三大特性:封裝、繼承、多態 一.封裝 【註】封裝就是把某些解決一些問題的代碼進行模塊化,對外部暴露一個介面。我們都稱為封裝了一個方法; 【優點】 1.封裝可以增強代碼的復用度。 2.封裝可以有效提高開發速度。 二.繼承 子類擁有了父類的方法和屬性
  • 什麼是Quartz2D? Quartz 2D是一個二維圖形繪製引擎,支持ios環境和Mac OS X環境。我們可以使用Quartz 2D API來實現許多功能如基本 路徑的繪製、透明度、描影、繪製陰影、透明層、顏色管理、反鋸齒、PDF文檔生成和PDF元數據訪問。在需要的時候,Quartz 2D 還可
  • 在這個網路橫行的時代......... 有沒有小說的感覺,哈哈😄。 言歸正傳,之前我寫過XML的網路解析,但是現在的app開發很少有用到XML解析的了,主流的則是JSON。(有時間我會對其進行總結)三方更是讓json解析更加簡便,由於代碼過於簡單,請大家做好心理準備。不要被嚇到哦。
  • Activity的啟動模式有四種,分別是standard、singleTop、singleTask、singleInstance。 Android是通過回退棧的模式來管理Activity實例的。棧是一種先進後出的集合。比如我們打開一個Activity,當前顯示的就是在棧的最頂端,當用戶點擊後退或者是
  • 一個好的APP不僅有美觀,好看的界面,更需要良好的性能和穩定性。作為一名開發人員,需要理解界面設計原則並寫出優秀的界面設計代碼。 本章主要講述基本控制項的使用,界面佈局及一些常用的界面設計屬性。 (1) LinearLayout 相對佈局 android:android:layout_weight=”
  • 在之前我寫過SDWebImage的使用方法,主要是用與獲取網路圖片,沒有看過的朋友可以看看。 這篇文章將主要介紹SDWebImage的實現原理,主要針對於獲取網路圖片的原理,如果沒有第三方我們該怎麼去做,當然我知識用文字去介紹,我想花大把的時間去深入理解我們用不到的東西,是很不值得的,不過興趣的朋友
  • @@@描述 廣播接收器可以收到 Context.sendBroadcast或者Context.sendOrderedBroadcast發出的意圖(intent)。 @@@LocalBroadcastManager 如果你不需要誇應用發送廣播,建議你使用 LocalBroadcastManager, 
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...