如何为大型数据集创建UITableView索引

时间:2011-09-20 04:02:30

标签: iphone ios uitableview nsdictionary

我已经走下了索引,分段,桌面视图的兔子洞,并且被带走了,这让我想要破坏我房间里的许多有价值的物品...我希望最后一个简明扼要的问题是有人可以帮助我解决这个问题。

我将从头开始,以免混淆提供帮助的任何人,我认为这样更容易,希望有人可以发布一些相当不错的例子让我阅读/使用,就像以前的问题我认为我已经提供了很多细节,并且混淆了每个请求实际上很简单的地方..它只是实现了,取决于你进入的数据和方式。

好的,我们走了。

我有一个UITableView,在打开时查询我的数据库以获取汽车制造商列表,我解析出来的XML并留下未分类的汽车制造商列表,然后我将此NSArray传递给自定义方法然后将它排序AZ。

- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
    //Sort incoming array alphabetically
    [self setSortedArray:[arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];

从这里我想知道如何为大型数据库的索引滚动创建索引UITableview。<< ---- 这是我的问题

下面我所知道的,从这一点来说,我需要创建一个字母数组,代表我的数据中找到的字母表中的字母(即)排序数组中每个值的第一个字母。我还需要创建一个NSDictionary,将我的排序数组拆分成带有这个字典的部分。

从那里我需要更新我的一些UItableview委托方法,以便他们可以处理NSDictionary和部分等。从那里我需要将我的NSDictionary中的值传递给我的UItableviewcell标签......

在以前的所有尝试中,我已经达到了我拥有的所有东西,但实际上能够将数据显示到tableview单元格中,但是因为它通过阅读几个不同的教程而被放在一起它只是不起作用。所以我现在觉得我真的知道需要发生什么,也许有人会很友好地分享他们能够学习的类似问题的代码,并希望学会如何实际做到这一点......

我要感谢迄今为止帮助过我的任何人以及将来要帮助我的任何人:PI肯定会发表我的最终解决方案并发表评论,希望将来可以防止这种情况发生在其他任何人身上!对于这种实施方式,我不禁对苹果产生了一点点仇恨。

Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Honda,
    Mazda,
    Mazda,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Mitsubishi,
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Nissan,
    Toyota,
    Toyota,
    Toyota

1 个答案:

答案 0 :(得分:2)

通过阅读您的问题,听起来您希望基于大型数据库获得分段UITableView。你在想几个部分?它可以处理任意多个,但有一些功能你无法使用,如index - 如果你有100多个部分可能不是很好。 (事实上​​,我认为可能存在限制,但我不确定。)

在这种情况下最简单的方法是拥有一个数组数组。该数组将为每个部分保留一个数组。所以它会是这样的:

@interface myTableView: UITableViewController {
  NSMutableArray * arrayOfSection;
  NSMutableArray * sectionHeaders;
}

@implementation myTableView

// Note I will assume that arrayData contains only NSStrings.
- (void)sortSectionsWithArray:(NSMutableArray *)arrayData; {
  NSMutableArray * alphabet = [[NSMutableArray alloc] initWithObjects:@"A", @"B", ..., @"Y", @"Z", nil];
  NSString * firstLetter;
  for(NSString * string in arrayData){
    firstLetter = [string substringToIndex:1];
    for(NSString * sectionString in alphabet){
      if([firstLetter isEqualToString:sectionString]){
        [alphabet removeObject:sectionString];
        break;
      }
    }
  }
  // Note that whatever is left over are the letters of the alphabet that are NOT section headers, as we removed the section headers strings.
  // Therefore, if we remove the letters stored in the alphabet array, we have the remaining section headers!
  if(sectionHeaders){
    [sectionHeaders release];
  }
  sectionHeaders = [[NSMutableArray alloc] initWithObjects:@"A", @"B", ..., @"Y", @"Z", nil];
  for(NSString * string in sectionHeaders){
    for(NSString * sectionString in alphabet){
      if([string isEqualToString:sectionString]){
        [sectionHeaders removeObject:string];
        break;
      }
    }
  }
}

// Assume that the array below is your input array:
// NSArray * arrayData = [[NSArray alloc] initWithObjects:@"Honda", @"Honda", @"Mazda", @"Mazda", @"Mitsubishi", @"Mitsubishi", @"Nissan", @"Nissan", @"Toyota", @"Toyota", nil];
- (void)startSortingTheArray:(NSMutableArray *)arrayData; {
  if(arrayOfSection){
    [arrayOfSection release];
  }
  arrayOfSection = [[NSMutableArray alloc] initWithCapacity:[sectionHeaders count]];
  NSMutableArray * section;
  for(NSString * string in sectionHeaders){
    section = [[NSMutableArray alloc] init];
    for(NSString * dataString in arrayData){
      if([string isEqualToString:[dataString substringToIndex:1]]){
        [section addObject:dataString];
      }
    }
    [arrayOfSection addObject:section];
    [section release];
  }
}

然后,在UITableViewDelegate方法中,使用:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
  return [[arrayOfSection objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
  static NSString * CellIdentifier = @"TableViewCell";
  UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
  }
  // Set model saved in arrayOfSection by using: [[arrayOfSection objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; {
  // What you want the view controller to do when a row is selected.
}

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

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; {
  return sectionHeaders;
}

- (void)dealloc; {
  [arrayOfSection release];
  [sectionHeaders release];
  [super dealloc];
}

希望有帮助!