【问题标题】:How to load the right nibs when selecting a sorted row?选择排序行时如何加载正确的笔尖?
【发布时间】:2011-07-27 13:27:24
【问题描述】:

我按照这个答案How to sort alphabetically a UITableView Sectioned? 对 UITableView 进行排序。

现在的问题是 UITableView 没有推出正确的 ViewController,因为我的应用程序中有 2 个本地化版本,英语和意大利语。对于英语它可以正常工作,但对于意大利语则不行。

我正在使用的代码是这个,正如这个答案Pushing View Controllers in UITableViewController Grouped中所建议的那样:

if (indexPath.section == 0) 
{   
     switch (indexPath.row) {
        case 0:
        [self.navigationController pushViewController:[[[FirstViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
        break;

        case 1:
        [self.navigationController pushViewController:[[[SecondViewController alloc] initWithNibName:nil bundle:nil] autorelease] animated:YES];
        break;
        // and so on
    }
} 

对此有什么想法吗?

【问题讨论】:

  • 如果您对行进行排序/更改它,您还应该在选择行时更改关联的视图控制器。
  • 是的,但问题是当我推出英语控制器时,它可以工作。当我切换到意大利语时,它不再起作用了。我忘了说我有 NSLocalizedStrings,用于本地化。
  • 你的意思是我创建这个应用程序是为了什么?
  • 是的,当您在应用程序中指的是英语/意大利语以及类似的东西时,这会令人困惑,而 iPhone 具有内置的语言功能。那么这个应用有什么用呢?
  • 我的意思是本地化。我的应用程序有 2 个本地化。英语和意大利语!顺便说一句,该应用程序是按类别细分的提示和技巧列表。该行的文本已本地化为英语和意大利语。你知道我的意思吗?

标签: objective-c cocoa-touch uitableview


【解决方案1】:

您的问题完全合乎逻辑,不需要更改您的代码。当您对tableView 数组进行排序时,didSelectRowAtIndexPath 在选择行时会变得混乱,这是微不足道的。

这可能是因为你会实现类似的东西

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if (indexPath.section == 0)
   {
    if (indexPath.row ==0)
         // PushViewController to view 1 //

    if (indexPath.row ==1)
         // PushViewController to view 2 //

   }

if (indexPath.section ==1)
   // Do something like before

如果这是您的情况,那么是的,当您对 tableView 进行排序时,您将以错误的方式推送视图控制器。

我的建议是,您可以在每一行中获取一些属性,并在推送视图控制器时检查这些属性。例如,如果您...(比如说)第 1 行是某个字母、数字或单词,您可以检查它并相应地 pushViewController。

但这只有在您知道tableView 中的所有元素时才有效(不要介意排序)。

你可以检查类似...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

 if (selectedCell.textLabel.text = @"1")
   // push view controller to view 1

 if (selectedCell.textLabel.text isEqualToString: self.lifeStyle) 
    // push view controller to lifeStyleViewController

    // here lifeStyle is a NSString property // 

如果您在cellForRowAtIndexPath 方法中逻辑设置和分配属性并逻辑设置它以便在排序后执行tableView reloadData 时,您可以检查任何内容,didSelect 的流程仍然相同.

如果您仍有问题,请告诉我们。

【讨论】:

  • 很好的答案,但是您投保的代码应该放在 if (indexPath.section == 0) 之后,即在开关内部,或者 textLabel.text 已经意味着它可以识别部分中的单元格?顺便谢谢你!
  • 和字符串比较应该是固定的;)
【解决方案2】:

我可能是错的,但我很确定你的排序,关于英语和意大利语,会以不同的顺序排列单元格。因此,您必须使用 nibs 引用相应地对另一个数组进行排序。我猜你的英文第一行和意大利的不一样,还是我错了?

【讨论】:

  • 你是完全正确的,事实上,如果我为英语加载第一个笔尖,例如,意大利语是第三个。
猜你喜欢
  • 1970-01-01
  • 2012-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多