Cocoa Touch->Objective-C class->Class:ViewController,Subclass of:UIViewController 2.打開xib..."/>
您好,登錄后才能下訂單哦!
效果:
步驟:
1.創(chuàng)建一個ViewController,New File->Cocoa Touch->Objective-C class->Class:ViewController,Subclass of:UIViewController
2.打開xib,在view中添加TableView,并將TableView的兩個屬性拖到File's Owner中,
可以設(shè)置tableview的分區(qū)樣式,選擇style
3.ViewController.h:
#import <UIKit/UIKit.h> @interface newViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (retain, nonatomic) IBOutlet UITableView *tableView; @property(retain,nonatomic)NSDictionary *dic; @property(retain,nonatomic)NSArray *keys; @end4.ViewController.m:
#import "newViewController.h" @interface newViewController () @end @implementation newViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"]; self.dic = [NSDictionary dictionaryWithContentsOfFile:path]; self.keys = [self.dic allKeys]; self.keys = [self.keys sortedArrayUsingSelector:@selector(compare:)]; } //tableView有多少分區(qū) -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.keys count]; } //每個分區(qū)對應(yīng)多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *key = self.keys[section];//通過第幾個分區(qū)拿到對應(yīng)的key NSArray *arr = [self.dic objectForKey:key];//通過這個key獲得對應(yīng)的Array return [arr count]; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *str = @"str"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]; } int section = [indexPath section]; int row = [indexPath row]; NSString *key = self.keys[section]; NSArray *arr = [self.dic objectForKey:key]; cell.textLabel.text = arr[row]; return cell; } //在每個分區(qū)上顯示什么內(nèi)容 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return self.keys[section]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //設(shè)置索引 -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return self.keys; } - (void)dealloc { [_tableView release]; [self.keys release]; [self.dic release]; [super dealloc]; } @end
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。