UITablView索引列表

 2023-09-15 阅读 26 评论 0

摘要:// .h 文件 #import <UIKit/UIKit.h>#define kDeviceHeight [UIScreen mainScreen].bounds.size.height;@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { @privateUITableView *_tableView;NSArray *
// .h 文件 
#import <UIKit/UIKit.h>#define kDeviceHeight [UIScreen mainScreen].bounds.size.height;@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
@privateUITableView *_tableView;NSArray     *_listArray;NSDictionary *_dataDic;
}
@end

// .m 文件 
#import "RootViewController.h"
#import "DetailViewController.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization}return self;
}- (void)loadView {// 创建一个基本视图UIView  *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];// 设置为当前控制器的默认视图self.view = view;// 释放视图[view release];NSString *path = [[NSBundle mainBundle] pathForResource:@"ListData" ofType:@"plist"];_dataDic = [[NSDictionary dictionaryWithContentsOfFile:path] retain];NSArray *keyArray = [[NSArray arrayWithArray:[_dataDic allKeys]] retain];_listArray = [[keyArray sortedArrayUsingSelector:@selector(compare:)] retain];NSLog(@"%@", _listArray);// 创建tableview_tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];// 设置数据源 注意这个方法设置不对,没有数据哦!_tableView.dataSource = self;// 设置表格的代理_tableView.delegate = self;// 设置表视图cell的高度_tableView.rowHeight = 70;// 添加到默认控制器中[self.view addSubview:_tableView];}// 表视图sescations有多少个
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {return [_listArray count];
}// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {NSArray *arr = [_dataDic objectForKey:[_listArray objectAtIndex:section]];return [arr count];
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{// 定义一个静态标识符static NSString *cellIdentifier = @"cell";// 检查是否限制单元格UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];// 创建单元格if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];}// 给cell内容赋值NSArray *data = [_dataDic objectForKey:[_listArray objectAtIndex:indexPath.section]];NSString *fontName = [data objectAtIndex:indexPath.row];cell.textLabel.text = fontName;cell.textLabel.font = [UIFont systemFontOfSize:18];return cell;}// 索引主标题- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{return _listArray[section];
}// 副标题- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{return _listArray;
} // 返回索引的内容- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{NSLog(@"index : %d title : %@", index, title);return index;
} // 选中时,跳转表视图- (void) dealloc {[_listArray release];[super dealloc];
}
- (void)viewDidLoad
{[super viewDidLoad];// Do any additional setup after loading the view.
}- (void)didReceiveMemoryWarning
{[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
// ListData.plist<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict><key>A</key><array><string>Australia</string><string>Argentina</string><string>American</string><string>Afghanistan</string></array><key>B</key><array><string>Brazil</string><string>Belgium</string><string>Bermuda</string></array><key>C</key><array><string>China</string><string>Canada</string><string>Congo</string><string>Cameroon</string><string>Chad</string></array><key>D</key><array><string>Denmark</string><string>Djibouti</string></array><key>E</key><array><string>England</string><string>Egypt</string><string>Estonia</string></array><key>F</key><array><string>France</string><string>Finland</string></array><key>G</key><array><string>Germany</string><string>Greece</string><string>Georgia</string><string>Guinea</string></array>
</dict>
</plist>

转载于:https://my.oschina.net/wangdk/blog/151778

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/56262.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息