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

标签 iphone ios uitableview nsdictionary

我已经进入了索引、分段、表格 View 的兔子洞,并被带上了一段让我想破坏我房间里很多贵重元素的旅程……我希望最后一个清晰简洁的问题是有人将能够帮助我解决这个问题。

我将从头开始,以免混淆提供帮助的任何人,我认为这样更容易,希望有人可以发布一些相当不错的示例供我阅读/使用,就像我认为我之前的问题一样提供了很多细节,让每个人都感到困惑,因为请求实际上很简单。它只是制定了实现,并取决于您输入的数据以及输入的方式。

好的,我们开始吧。

我有一个 UITableView,当它打开时查询我的数据库以获取汽车制造商列表,我解析进来的 XML 并留下一个未排序的汽车制造商列表,然后我将这个 NSArray 传递给一个自定义方法,然后按 A-Z 排序。

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

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

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

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

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

我要感谢迄今为止帮助过我的任何人以及 future 将要帮助我的任何人 :P 我一定会发布我的最终解决方案并附上评论,希望能防止这种情况在未来发生在其他人身上!我忍不住对 apple 对这个实现的这个泡菜感到有点仇恨。

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

最佳答案

从阅读你的问题来看,听起来你想要一个基于大型数据库的分段 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];
}

希望对您有所帮助!

关于iphone - 如何为大型数据集创建 UITableView 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7479998/

相关文章:

iphone - 我应该在哪里创建和展示整个 iPhone 应用程序中使用的 Modal ViewController

iphone - MKStoreKit 是在应用程序订阅中用于自动更新的正确选项吗?

ios - 使用 MVVM swift 在 View DidLoad 上进行 Api 调用

iOS TableView(错误 : two scroll Bars Found As Zoomed)

iphone - iPad 2 上带有 HDMI 适配器的黑条

ios - 如何在 iOS(例如 iPhone)中保存 .plist 文件?

ios - 改变 Sprite 生成

iphone - 如何使用一次触摸更改 UISlider 值?

ios - 如何检查 UITableView 何时完成滚动

iOS - 当 TextView 位于表格 View 单元格中时将其滚动到顶部