【问题标题】:How to create UITableView Index for large datasets如何为大型数据集创建 UITableView 索引
【发布时间】:2011-09-20 04:02:29
【问题描述】:

我已经陷入了索引、分段、tableviews 的兔子洞,并且被带到了一个让我想要损坏我房间里很多有价值的物品的旅程......我希望最后一个清晰简洁的问题是有人将能够帮助我解决这个问题。

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

好的,我们开始吧。

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

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

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

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

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

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

我要感谢迄今为止帮助过我的任何人以及将来将帮助我的任何人:P 我一定会在 cmets 上发布我的最终解决方案,以希望将来防止这种情况发生在其他人身上!对于这种腌制的实现,我不禁对苹果产生了轻微的仇恨。

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

【问题讨论】:

    标签: iphone ios uitableview nsdictionary


    【解决方案1】:

    通过阅读您的问题,听起来您想要基于大型数据库进行分段 UITableView。你想几个部分?它可以处理任意数量的内容,但有些功能您将无法使用,例如 index-ing 如果您有 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];
    }
    

    希望有帮助!

    【讨论】:

    • 我会好好阅读你的代码。但只是为了回答你的第一个问题。显然我没有再次很好地解释它。但我在我的数据库中获取每个单词的第一个字符并检查它是什么,如果它在 A-Z 范围内,那么那个字母就变成了一个部分......如果它们是同一部分的多个字母,则只会制作一个部分。 . 基本上我只有在我的数据中出现的字母部分。
    • 如果您只将 A-Z 作为部分标题,那么这将更加容易。我也会发布一个方法来做到这一点,请稍等。
    • 是的,但并非所有字母都会被表示,只有那些在我的数据集中使用的字母:P .. 期待您的解决方案
    • 伙计,我一直在努力工作,但老实说,我很迷茫。我打勾是因为我确信它是正确的,但我就是无法让它发挥作用……我对这整个血腥事件感到非常沮丧。
    • 你为什么不发布一些你的数据集,我再研究一下?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2020-08-27
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    相关资源
    最近更新 更多