您好,登錄后才能下訂單哦!
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//創(chuàng)建tableview
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
//設置索引字體的顏色
tableView.sectionIndexColor = [UIColor greenColor];
//設置索引背景顏色
tableView.sectionIndexBackgroundColor = [UIColor redColor];
//設置點擊后索引背景的顏色
tableView.sectionIndexTrackingBackgroundColor = [UIColor blackColor];
//獲取文件路徑
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ListData" ofType:@"plist"];
self.dataDic = [NSDictionary dictionaryWithContentsOfFile:filePath];
allKeys = [self.dataDic allKeys];
//排序
allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//返回組數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return allKeys.count;
}
//返回每組的行數(shù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *cellArray =[self.dataDic objectForKey:allKeys[section]] ;
return cellArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
NSArray *cellArray =[self.dataDic objectForKey:allKeys[indexPath.section]] ;
cell.textLabel.text = [cellArray objectAtIndex:indexPath.row];
return cell;
}
//section標題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return allKeys[section];
}
//索引
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return allKeys;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。