2015-03-02 55 views
0

我是新来的Objective C,我试图在我的tableView中指定numberOfSectionsInTableView这里是我的代码。填充numberOfSectionsInTableView使用NSMutableArray

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

[_dateSectionTitles count]总是返回零。

这是我如何得到_dateSectionTitles

- (void) dictionaryItems { 
    NSMutableDictionary* dates = [NSMutableDictionary dictionary]; 
    _dateSectionTitles = [[NSMutableArray alloc] init]; 
    for (myObject *cItem in _cashArray) { 
     NSString* dateString = [cItem.date stringFormattedWithDayAndFullMonth]; 
     array = [dates valueForKey: dateString]; 
     if(!array) 
     { 
      array = [@[cItem] mutableCopy]; 
      [dates setObject:array forKey: dateString]; 
      [_dateSectionTitles addObject:dateString]; 
     } 
     else 
     { 
      [array addObject: cItem]; 
     } 
    } 
} 

回答

1

从一起简单了解一下,它看起来像array从未声明。这意味着您的条件的else语句始终被执行,并且没有任何内容被添加到dateSectionTitles

NSMutableArray *array = [dates valueForKey: dateString]; 
0

根据你的代码,你永远不会实例化你的array变量。你需要有有这样的地方:

NSMutableArray *array = [[NSMutableArray alloc] init]; 
+0

它在我的'@接口视图控制器(){ 的NSMutableArray *数组声明; }' – 2015-03-02 18:57:33

+0

那还不够。根据你的**其他**你必须初始化它或不会添加任何对象。这可能是正在发生的事情。声明它是不够的,你需要初始化它使用** [array addobject:cItem] ** – Chase 2015-03-02 19:00:28

+0

也在你的if - else .... if数组= [日期valueForString:dateString]总是返回一个数组永远不会被添加到_dateSectionTitles,因为只有你的else语句会被执行。 – Chase 2015-03-02 19:05:58