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

发布于 2024-12-05 08:10:54 字数 1499 浏览 0 评论 0原文

我已经陷入了索引、分区、桌面视图的兔子洞,并被带上了一次让我想损坏我房间里很多有价值的物品的旅程……我希望最后一个清晰简洁的问题有人会能够帮助我解决这个持续的斗争。

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

好的,我们开始吧。

我有一个 UITableView,当打开它时,它会查询我的数据库中的汽车制造商列表,我解析传入的 XML,并留下未排序的汽车制造商列表,然后我将此 NSArray 传递给一个自定义方法,然后对其进行排序 AZ 。

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

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

这是我所知道的,从这一点来看我需要创建一个字母数组,表示在我的数据中找到的字母表中的字母(即排序数组中每个值的第一个字母)。我还需要创建一个 NSDictionary,将排序后的数组分成该字典中的多个部分。

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

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

我想感谢迄今为止帮助过我的任何人以及将来任何将要帮助我的人: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

I have gone down the rabbit hole of indexed, sectioned, tableviews and have been taken on a ride that has made me want to damage alot of valuable objects in my room... I am hoping with one last clear concise question that someone will be able to help me solve this on going struggle.

Im going to start from the beginning so as not to confuse anyeon that offers their help, I think its easier that way and hopefully someone can post some fairly decent examples for me to read/use, as in previous questions I think I have supplied to much detail and confused everyone where the request is actually simple.. its just the implementation is drawn out and dependant on what data you have coming in and how.

Okay here we go.

I have a UITableView which when opened queries my database for a list of car manufactures, I parse the XML that comes in and am left with a unsorted list of car manufactures, I then pass this NSArray over to a custom method that then sortes it A-Z.

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

From here I would like to know how to make a Indexed UITableview for Index Scrolling of a large database.. <<---- This is my question

Heres what I know, From this point I need to create an array of letters that represent the letters of the alphabet thats found in my data (i.e.) first letter of each value in my sorted array. I also need to create an NSDictionary with my sorted arrays split into sections withing this dictionary.

From there I need to update some of my UItableview delegate methods so that they can handle the NSDictionary and sections etc.. From there I need to pass the values in my NSDictionary over to my UItableviewcell labels...

In all previous attempts I have gotten to the point where I have everything but actually being able to display the data into the tableview cells, but because its been put together by reading several different tutorials its just not working. So I figured now that I actually kinda know what needs to happen maybe someone will be kind enough to share their code of a similar problem that I will be able to learn from and hopefully learn how to actually do this...

I would like to thank anyone that has helped me so far and anyone who is about to help me in the future :P I will be sure to post my final solution with comments to hopefully prevent this from happening to anyone else in the future! I cannot help but feel a slight amount of hatred towards apple for this pickle of an implementation.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一百个冬季 2024-12-12 08:10:54

从阅读你的问题来看,听起来你想要一个基于大型数据库的分段 UITableView 。你想分几个部分?它可以处理任意数量的数据,但有些功能您将无法使用,例如如果您有 100 多个部分,则 index-ing 可能不太好。 (事实上​​,我认为这可能有限制,但我不确定。)

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

@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];
}

希望有帮助!

From reading your question, it sounds like you want to have a sectioned UITableView based on a large database. How many sections are you thinking? It can handle as many as you want, but there are some features which you will not be able to use, like the index-ing may not be great if you have 100+ sections. (In fact, I think there might be a limit on this, but I'm not sure.)

The easiest thing to do in this case would be to have an array of arrays. The array would hold an array for each section. So it would be something like:

@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];
  }
}

Then, in your UITableViewDelegate methods, use:

- (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];
}

Hope that Helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文