iOS tableView索引

来源:互联网 发布:淘宝回购率 编辑:程序博客网 时间:2024/05/16 09:53

其实tableView的索引的实现非常简单,主要是靠下面这个方法再结合多分区实现的:

//建立浮动索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return [NSArray arrayWithObjects:@"S",@"H",@"S"nil];

}


以下的代码中的data是一个字典,indexs是字典的key所组成的数组。用字典中的数组来填充多分区中的数据。


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

   return [indexscount];

    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

   NSString *secNum = [indexsobjectAtIndex:section];

   return [[dataobjectForKey:secNum] count];

}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString *cellId =@"cell";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

   if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];

    }

   NSInteger rowNo = indexPath.row;

   NSInteger rowSe = indexPath.section;

    cell.textLabel.text = [[dataobjectForKey:[indexsobjectAtIndex:rowSe]] objectAtIndex:rowNo];

   return cell;

    

}

//建立浮动索引

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return [NSArrayarrayWithObjects:@"S",@"H",@"S",nil];

}

//分区页眉

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

   return  [indexsobjectAtIndex:section];

}

//分区页脚

//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

//    NSString *str = [NSString stringWithFormat:@"一共有%lu个人物",(unsigned long)[[data objectForKey:[indexs objectAtIndex:section]] count]];

//    return str;

//}


0 0