【问题标题】:iOS titleForHeaderInSection crashiOS titleForHeaderInSection 崩溃
【发布时间】:2012-01-04 20:26:55
【问题描述】:

问题是这两个部分有时是空的,所以我不知道如何防止它们变空时崩溃。 self.globalSections 是由本地两个 NSMutableArrays 存储的全局 NSMutableArrays,它们是我的 UITableView 的第一和第二部分。

这是我的代码:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{

    if (mySearchBar.text.length > 0)
    {
        if(section == 0)
        {
            //below are if statements I have tried, but not working...
            //NSUInteger globalSectionsOne = [self.globalSections indexOfObject:[self.globalSections objectAtIndex:0]];
            //if ([[self.globalSections objectAtIndex:0]count] >=1)
            //  if (globalSectionsOne!=NSNotFound)
            //if (globalSectionsOne >= 1)

            //{
                NSLog(@"sections counts");
                NSUInteger firstSectionCount = [[self.globalSections objectAtIndex:0]count];

                NSString *firstSectionString = [NSString stringWithFormat:@"%i Exact match(es)", firstSectionCount];

                return firstSectionString;
            //}
            //else {return @"";}

        }
        else if (section == 1)
        {
 //another same code, except the objectAtIndex is at 1 instead of 0.

请指出正确的方向,谢谢。

编辑:

我在这个方法中做了修改:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    if (mySearchBar.text.length > 0 && [self.globalSections count] == 2)
    {
        return 2;
    }
    else 
    {
        return 1;
    }

    //return 1;
}

这样好吗?

【问题讨论】:

  • 使用崩溃日志更新您的问题。

标签: ios uitableview title


【解决方案1】:

您可以通过这种方式避免在titleForHeaderInSection: 中重复代码:

if (mySearchBar.text.length > 0)
{
    if ([self.globalSections count] > section)
    {
        int sectionCount = [[self.globalSections objectAtIndex:section] count];
        NSString *matchWord = (sectionCount == 1) ? @"match" : @"matches";
        return [NSString stringWithFormat:@"%i Exact %@", sectionCount, matchWord];
    }
    else
    {
        return @"";
    }
}

【讨论】:

  • 我尝试了你的代码,但我仍然收到如下编译器警告:*** 由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'*** -[NSMutableArray objectAtIndex:]: index 0超出空数组的界限'
  • 你能解释一下是什么吗?和:在 NSString *matchWord = (sectionCount == 1) 中做吗? @"match" : @"matches";?
  • ?: 是terniary operator。当存在 if/else 条件时,这只是为变量赋值的一种更紧凑的方式。当括号中的条件为真时,表达式计算为冒号左边的值,否则计算为右边的值。
  • 我刚刚查看了您在上面的原始问题中添加的崩溃日志。它显示错误来自MainViewController tableView:numberOfRowsInSection:,而不是titleForSectionInHead:。查看numberOfRowsInSection: 中的代码,并确保在调用objectAtIndex 之前检查NSMutableArray 的count 是否>0。
  • 感谢您教我三元运算符一词,它是节省空间的代码:) 你是对的,我今天早上注意到它并解决了我的问题。感谢您抽出宝贵时间帮助我。
【解决方案2】:

永远不要索引你不知道大小的数组。在调用objectAtIndex: 之前,始终调用count 或通过合约确保数组有元素。在这种情况下,您需要准确表示在numberOfSectionsInTableView: 的实现中将显示多少个非空部分。

如果您要返回 globalSections 数组中包含的数组数量的准确计数,您将永远不会遇到上述情况。

【讨论】:

  • 所以你是说我应该这样写: if ( [self.globalSections count] >= 2) 在我在 titleForHeaderInSection 方法中开始这段代码之前? (我不确定 count 是从零开始还是从 1 开始,如果它包含一个对象)
  • 它是从零开始的。是的,你可以做这样的检查,如果不正确则返回 nil。
  • 没关系,我发现真正的问题不是这个 :) 感谢您抽出宝贵时间帮助我。
猜你喜欢
  • 2011-08-26
  • 2017-06-12
  • 2018-03-27
  • 2021-04-17
  • 2013-05-13
  • 2018-12-02
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多