詳解iPhone開發應用之表視圖分組實現代碼
作者:佚名
本文介紹的是iPhone開發應用中表視圖分組的實現,主要是以代碼實現視圖的分組內容,先來看本文詳細講解。
iPhone開發應用中表視圖分組的實現是本文要介紹的內容,主要是以代碼實現視圖的分組,不多說,先來看本文詳細內容,貼一張圖:
1.先創建 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.實現代碼
- @implementation SectionsViewController
- @synthesize names;
- @synthesize keys;
- - (void)viewDidLoad {
- NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames"
- ofType:@"plist"]; //獲取屬性列表的路徑,賦給path
- NSDictionary *dict=[[NSDictionary alloc]
- initWithContentsOfFile:path]; //將 路徑path下的數據表 初始化字典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
- // 返回有多少個分區
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return [keys count]; //獲取分區的數量
- }
- //返回 每個分區 有多少行
- - (NSInteger)tableView:(UITableView *)tableView
- numberOfRowsInSection:(NSInteger)section
- {
- NSString *key = [keys objectAtIndex:section]; //section為其中一個分區,獲取section的索引
- NSArray *nameSection = [names objectForKey:key]; //根據索引獲取分區里面的所有數據
- return [nameSection count]; //返回分區里的行的數量
- }
- //返回 當前需要顯示的cell, 可能是 為了節省內存
- - (UITableViewCell *)tableView:(UITableView *)tableView
- cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- //NSLog(@"tianshi\n");
- NSUInteger section = [indexPath section];//返回第幾分區
- NSUInteger row = [indexPath row];//獲取第幾分區的第幾行
- NSString *key = [keys objectAtIndex:section]; //返回 分區的索引key
- NSArray *nameSection = [names objectForKey:key];//返回 根據key獲得:當前分區的所有內容,
- 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;
- }
- //為每一個分區指定一個名稱,現在的名稱為key的值
- - (NSString *)tableView:(UITableView *)tableView
- titleForHeaderInSection:(NSInteger)section
- {
- NSString *key = [keys objectAtIndex:section];
- return key;
- }
- //添加索引的值,為右側的A----E
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
- {
- return keys;
- }
小結:詳解iPhone開發應用之表視圖分組實現代碼的內容介紹完了,希望本文對你有所幫助!
責任編輯:zhaolei
來源:
互聯網