浅谈ios设计之使用表格UITableVIew设计通讯录的方法
生活随笔
收集整理的這篇文章主要介紹了
浅谈ios设计之使用表格UITableVIew设计通讯录的方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路;首先自作一份或者網上下載一份通訊錄的 ? plist ? 文件 之后創建工程和顯示通訊錄的表格 代碼實現如下(全部代碼) 1、創建導航欄和根視圖 AppDelegate.h #import <UIKit/UIKit.h>
#import "SortednameTableViewController.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 {
?? //創建根視圖(將SortednameTableViewController作為根視圖)(UINavigationController 用于創建導航欄)
??? self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[SortednameTableViewController alloc]initWithStyle:UITableViewStyleGrouped]];
??? return YES; } 2、表格部分(就是創建的類 繼承UITableViewController) SortednameTableViewController.h
#import <UIKit/UIKit.h>
@interface SortednameTableViewController : UITableViewController
//接收文件的字典
@property(strong,nonatomic)NSDictionary *Dictionary;
//接收字典關鍵字(A~Z)的集合
@property(strong,nonatomic)NSArray *KeysArray; @end SortednameTableViewController.m
#import "SortednameTableViewController.h"
@interface SortednameTableViewController ()
@end
@implementation SortednameTableViewController
- (void)viewDidLoad {
??? [super viewDidLoad];
? //設置視圖背景色
??? self.view.backgroundColor=[UIColor yellowColor];
??? //頁面標題
??? self.title=@"通訊錄";
???
??? //導入存通訊錄的plist文件的路徑
??? NSString? *path=[[NSBundle mainBundle]pathForResource:@"sortednames" ofType:@"plist"];
??? //接收通訊錄文件
??? self.Dictionary=[NSDictionary dictionaryWithContentsOfFile:path];
??? //對字典中的關鍵字進行排序(選擇器排序法)
??? self.KeysArray=[self.Dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
???
??? //重用表格唯一標識
??? [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
?
}
- (void)didReceiveMemoryWarning {
??? [super didReceiveMemoryWarning];
??? // Dispose of any resources that can be recreated.
}
//表格分區數目
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
??? return self.KeysArray.count;//關鍵字的數量
}
//每個分區的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
??? NSString *key=self.KeysArray[section];
??? NSArray *arr=self.Dictionary[key];//獲取關鍵字對應的Value值
???
???
??? return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
??? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
??? NSString *key=self.KeysArray[indexPath.section];//分區的關鍵字
??? NSArray *keyArray=self.Dictionary[key];//關鍵字對應的value值
??? cell.textLabel.text=key;
??? cell.textLabel.text=keyArray[indexPath.row];
??? cell.textLabel.textAlignment=NSTextAlignmentCenter;//對齊方式
??? cell.textLabel.textColor=[UIColor blueColor];//字體顏色
???
??? return cell;
}
//設置分區標題
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
??? UILabel *label=[[UILabel alloc]init];
??? label.backgroundColor=[UIColor redColor];
??? label.text=self.KeysArray[section];//分區標題為分區對應的關鍵字
??? label.font=[UIFont systemFontOfSize:30];
??? label.textAlignment=NSTextAlignmentCenter;
??? return label;
}
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
??? return self.KeysArray;//顯示(A~Z)
}
//設置每個分區開頭
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
??? NSString *str=self.KeysArray[section];
??? return str;
}
//設置每個分區結尾標題
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
?? return @"end";
}
//設置分區高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
???
??? return 50; } 3、效果圖?
#import "SortednameTableViewController.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 {
?? //創建根視圖(將SortednameTableViewController作為根視圖)(UINavigationController 用于創建導航欄)
??? self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[SortednameTableViewController alloc]initWithStyle:UITableViewStyleGrouped]];
??? return YES; } 2、表格部分(就是創建的類 繼承UITableViewController) SortednameTableViewController.h
#import <UIKit/UIKit.h>
@interface SortednameTableViewController : UITableViewController
//接收文件的字典
@property(strong,nonatomic)NSDictionary *Dictionary;
//接收字典關鍵字(A~Z)的集合
@property(strong,nonatomic)NSArray *KeysArray; @end SortednameTableViewController.m
#import "SortednameTableViewController.h"
@interface SortednameTableViewController ()
@end
@implementation SortednameTableViewController
- (void)viewDidLoad {
??? [super viewDidLoad];
? //設置視圖背景色
??? self.view.backgroundColor=[UIColor yellowColor];
??? //頁面標題
??? self.title=@"通訊錄";
???
??? //導入存通訊錄的plist文件的路徑
??? NSString? *path=[[NSBundle mainBundle]pathForResource:@"sortednames" ofType:@"plist"];
??? //接收通訊錄文件
??? self.Dictionary=[NSDictionary dictionaryWithContentsOfFile:path];
??? //對字典中的關鍵字進行排序(選擇器排序法)
??? self.KeysArray=[self.Dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
???
??? //重用表格唯一標識
??? [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
?
}
- (void)didReceiveMemoryWarning {
??? [super didReceiveMemoryWarning];
??? // Dispose of any resources that can be recreated.
}
//表格分區數目
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
??? return self.KeysArray.count;//關鍵字的數量
}
//每個分區的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
??? NSString *key=self.KeysArray[section];
??? NSArray *arr=self.Dictionary[key];//獲取關鍵字對應的Value值
???
???
??? return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
??? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
??? NSString *key=self.KeysArray[indexPath.section];//分區的關鍵字
??? NSArray *keyArray=self.Dictionary[key];//關鍵字對應的value值
??? cell.textLabel.text=key;
??? cell.textLabel.text=keyArray[indexPath.row];
??? cell.textLabel.textAlignment=NSTextAlignmentCenter;//對齊方式
??? cell.textLabel.textColor=[UIColor blueColor];//字體顏色
???
??? return cell;
}
//設置分區標題
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
??? UILabel *label=[[UILabel alloc]init];
??? label.backgroundColor=[UIColor redColor];
??? label.text=self.KeysArray[section];//分區標題為分區對應的關鍵字
??? label.font=[UIFont systemFontOfSize:30];
??? label.textAlignment=NSTextAlignmentCenter;
??? return label;
}
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
??? return self.KeysArray;//顯示(A~Z)
}
//設置每個分區開頭
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
??? NSString *str=self.KeysArray[section];
??? return str;
}
//設置每個分區結尾標題
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
?? return @"end";
}
//設置分區高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
???
??? return 50; } 3、效果圖?
轉載于:https://www.cnblogs.com/guiyangxueyuan/p/5293068.html
總結
以上是生活随笔為你收集整理的浅谈ios设计之使用表格UITableVIew设计通讯录的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 统计十进制数的二进制数1的个数
- 下一篇: 89.数字三角形