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