【问题标题】:indexPath.row Returns Random Cell TextLabel in didSelectRowAtIndexPathindexPath.row 在 didSelectRowAtIndexPath 中返回随机单元格 TextLabel
【发布时间】:2012-07-23 14:47:09
【问题描述】:

我有一个由 SQLite 数据库填充的 UITableView。 I added Section-based Grouping using the sectionIndexTitlesForTableView delegate method today and now when a Cell is selected, the String for indexPath.row is not the same as the text in the selected Cell.

我的代码是这样工作的。

  1. 我创建了一个数组来保存 SQLite 数据库中的业务。
  2. 我按字母顺序对该数组进行排序。
  3. 我只使用数据库中企业以字母开头的字母创建了一个字母数组。
  4. 我使用该 Array 和 NSPredicate 来提供 Grouped Header 视图,这些视图按业务的首字母按字母顺序进行分组。
  5. 将选定行写入 NSUserDefaults 文件,并推送视图控制器 (iPhone),或为该键添加观察者 (iPad)。

不幸的是,由于添加了标题视图,indexPath.row 现在返回的字符串与所选 Cell 的 TextLabel 的字符串完全不同,因此显示了不同的 Business 信息。

这里是主数组的重要代码块。

- (void)viewDidLoad
{
 // Lots of code...
 arrayName = [[NSMutableArray alloc] init];
 NameSet = [[NSMutableSet alloc] init];
 sortedArray = [arrayName sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

 alphabet = [[NSMutableArray alloc] init];
 [alphabet addObject:@"{search}"];
 for (int i=0; i<[sortedArray count]-1; i++)
 {
  char alphabetUni = [[sortedArray objectAtIndex:i] characterAtIndex:0];
  NSString *uniChar = [NSString stringwithFormat:@"%c", alphabetUni];
  if (![alphabet containsObject:uniChar])
  {
    [alphabet addObject:uniChar];
  }
 }
 }

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

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
 NSInteger rows = 0;

 NSString *alpha = [alphabet objectAtIndex:section];

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
 businesses = [sortedArray filteredArrayUsingPredicate:predicate];

 if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    rows = [self.searchResults count];
 }
 else {
    rows = [businesses count];
 }

  return rows;
 }

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

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSString *CellIdentifier = @"Cell";
  cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:CellIdentifier];

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  }

  NSString *alpha = [alphabet objectAtIndex:indexPath.section];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alpha];
  businesses = [sortedArray filteredArrayUsingPredicate:predicate];

  if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    cell.textLabel.text =
    [self.searchResults objectAtIndex:indexPath.row];
  }
  else{
        NSString *cellValue = [businesses objectAtIndex:indexPath.row];
        cell.textLabel.text = cellValue;
  }
  return cell;

  }

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
 NSString *selected = nil;

 if (tableView == self.tableView)
 {
    selected = [businesses objectAtIndex:indexPath.row];
 }
 else if (tableView == searchDis.searchResultsTableView)
 {
    selected = [filteredData objectAtIndex:indexPath.row];
 }

 [def setObject:selected forKey:@"NameChoiceDetail"];

 if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
 {
    [self performSegueWithIdentifier:@"NameDetailPush" sender:self];

 }
 }

// 请原谅我写得很糟糕的代码。我只使用了 Objective-C 4 个月,而编程大约 8 个月。任何建议/优化都会被适当记录。

【问题讨论】:

  • 真的是随机的吗?尝试在与字符串有任何关系的每个步骤中添加 NSLogs。在您的 didSelectRowAtIndexPath 中,您正在执行 indexPath = [businesses objectAtIndex:indexPath.row],但在 cellForRow 中您正在执行 NSString *cellValue = [businesses objectAtIndex:indexPath.row] ,这看起来是错误的。部分没有被检查。 if (indexPath.section = someValue) 然后做一些具体的事情。表格视图中每个部分的 indexPath.row 值从零开始
  • 我不知道为什么要在其中设置 indexPath 值的断言。我想我正在涉足它,认为这会解决它。请放心,它不在当前(正在工作的)代码中,并且已被编辑出问题。

标签: iphone sqlite uitableview nspredicate didselectrowatindexpath


【解决方案1】:

您的表格视图使用部分,但您的tableView:didSelectRowAtIndexPath: 实现不评估索引路径的部分。所以代码缺少一些东西。

此外,我发现您对businesses 变量(它可能是一个实例变量)的使用非常奇怪。它在tableView:cellForRowAtIndexPath: 中分配了一个值,但在tableView:didSelectRowAtIndexPath: 中没有分配一个值,即使它在那里使用。因此,后者的结果取决于最后显示的表格单元格,因此它取决于滚动用户交互。这可能是结果看起来相当随机的原因。

【讨论】:

  • 非常感谢!!!通过在 didSelectRowAtIndexPath 方法中重新初始化“businesses”(这是一个 NSMutableArray)的值,我的选择现在可以被正确识别。
猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2011-07-08
  • 2016-12-20
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多