[课堂实践与项目]UITableView 使用.plist文件填充且实现分组和快速查找

 上一篇我们使用了viewDidLoad初始化数组的方式,现在就是用 dataplist的方式进行初始化。

 重点:1.plist的文件格式。


 2.文件的查找方式

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"];
    self.dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path ];
    self.array = [[self.dictionary allKeys]sortedArrayUsingSelector:@selector(compare:)];
    
   // self.dictionary = [NSBundle mainBundle]
    
    // Do any additional setup after loading the view from its nib.
}

3.关于cell的绘制的不同以及numberOfrows的改变

#pragma mark ---UItableViewDataSource   Methods---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.dictionary objectForKey:[self.array objectAtIndex:section]] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetifiter = @"CellIndetifiter";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifiter];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifiter];
    }
    
    NSInteger section = [indexPath section];
    
    NSArray *tempArray = [self.dictionary objectForKey:[self.array objectAtIndex:section]];
    
    
    cell.textLabel.text = [tempArray objectAtIndex:[indexPath row]];
    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    return  cell;
}

4.关于titleHeader的设置

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.array objectAtIndex:section];
}



5.根据key快速查找


- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.array;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值