詳解iPhone開發(fā)應用之表視圖分組實現(xiàn)代碼
作者:佚名
本文介紹的是iPhone開發(fā)應用中表視圖分組的實現(xiàn),主要是以代碼實現(xiàn)視圖的分組內(nèi)容,先來看本文詳細講解。
iPhone開發(fā)應用中表視圖分組的實現(xiàn)是本文要介紹的內(nèi)容,主要是以代碼實現(xiàn)視圖的分組,不多說,先來看本文詳細內(nèi)容,貼一張圖:
1.先創(chuàng)建 plist文件,
2.主界面 放置一個 table view控件
3.接口代碼
- @interface SectionsViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> {
- NSDictionary *names;
- NSArray *keys;
- }
- @property (nonatomic, retain) NSDictionary *names;
- @property (nonatomic, retain) NSArray *keys;
- 4.實現(xiàn)代碼
- @implementation SectionsViewController
- @synthesize names;
- @synthesize keys;
- - (void)viewDidLoad {
- NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames"
- ofType:@"plist"]; //獲取屬性列表的路徑,賦給path
- NSDictionary *dict=[[NSDictionary alloc]
- initWithContentsOfFile:path]; //將 路徑path下的數(shù)據(jù)表 初始化字典dict
- self.names = dict; //字典dict 賦給names
- //因為 names 有 retain 屬性。 當給names賦值時 ,dict會自動retain(增一),此時dict的retain count=2;
- [dict release];
- //for (int i=0; i<[[names allKeys] count]; i++) {
- // NSLog(@"%@\n",[[names allKeys] objectAtIndex:i]);
- // }
- //
- NSArray *array=[[names allKeys] sortedArrayUsingSelector:
- @selector(compare:)]; //給所有 keys 值按字母順序排序
- //for (int i=0; i<[array count]; i++) {
- // NSLog(@"%@\n",[array objectAtIndex:i]);
- // }
- self.keys = array; //將 array對象賦給 keys
- }
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- self.names = nil;
- self.keys = nil;
- }
- - (void)dealloc {
- [names release];
- [keys release];
- [super dealloc];
- }
- #pragma mark -
- #pragma mark Table View Data Source Methods
- // 返回有多少個分區(qū)
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return [keys count]; //獲取分區(qū)的數(shù)量
- }
- //返回 每個分區(qū) 有多少行
- - (NSInteger)tableView:(UITableView *)tableView
- numberOfRowsInSection:(NSInteger)section
- {
- NSString *key = [keys objectAtIndex:section]; //section為其中一個分區(qū),獲取section的索引
- NSArray *nameSection = [names objectForKey:key]; //根據(jù)索引獲取分區(qū)里面的所有數(shù)據(jù)
- return [nameSection count]; //返回分區(qū)里的行的數(shù)量
- }
- //返回 當前需要顯示的cell, 可能是 為了節(jié)省內(nèi)存
- - (UITableViewCell *)tableView:(UITableView *)tableView
- cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //NSLog(@"tianshi\n");
- NSUInteger section = [indexPath section];//返回第幾分區(qū)
- NSUInteger row = [indexPath row];//獲取第幾分區(qū)的第幾行
- NSString *key = [keys objectAtIndex:section]; //返回 分區(qū)的索引key
- NSArray *nameSection = [names objectForKey:key];//返回 根據(jù)key獲得:當前分區(qū)的所有內(nèi)容,
- static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
- //判斷cell是否存在,如果沒有,則新建一個
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
- SectionsTableIdentifier ];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
- reuseIdentifier: SectionsTableIdentifier ] autorelease];
- }
- //給cell賦值
- cell.textLabel.text = [nameSection objectAtIndex:row];
- return cell;
- }
- //為每一個分區(qū)指定一個名稱,現(xiàn)在的名稱為key的值
- - (NSString *)tableView:(UITableView *)tableView
- titleForHeaderInSection:(NSInteger)section
- {
- NSString *key = [keys objectAtIndex:section];
- return key;
- }
- //添加索引的值,為右側的A----E
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
- {
- return keys;
- }
小結:詳解iPhone開發(fā)應用之表視圖分組實現(xiàn)代碼的內(nèi)容介紹完了,希望本文對你有所幫助!
責任編輯:zhaolei
來源:
互聯(lián)網(wǎng)