NSArray - 无法从数组中获得价值

时间:2014-10-25 20:00:45

标签: ios objective-c arrays nsmutablearray

我在viewDidLoad中创建了一个可变数组,如下所示:

self.dayOrder=[NSMutableArray new];

NSCalendar *cal=[NSCalendar currentCalendar];

NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0,

for (int i=0;i<7;i++) {
    [self.dayOrder addObject:[NSNumber numberWithInteger:dayNumber]];
    dayNumber=(dayNumber+1)%6;
}

然后在cellForRowAtInxdexPath中,我想从这个数组中获取一个值,但是当我调用它时

NSNumber *dayNumber=self.dayOrder[indexPath.row];

然后我记录dayNumber它记录了一个数字99的列表。但是当我只记录数组它看起来很好吗?

感谢您的帮助。

我记录了这样的日期号码

NSLog(@"%d", dayNumber);

HERE是行方法的单元格

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//    NSNumber *dayNumber=self.dayOrder[indexPath.row];
    NSNumber *dayNumber= [self.dayOrder objectAtIndex:indexPath.row];

    NSInteger buttonNumber = [self.mealdata mealsForDay:indexPath.row].count;

//    NSLog(@"%d", dayNumber);

      NSLog(@"%d", self.dayOrder);

这是记录在cellForRow方法NSLog(@"%d", dayNumber);

中的内容
2014-10-25 15:56:24.683 U Eat[79938:2166115] 99
2014-10-25 15:56:24.687 U Eat[79938:2166115] 99
2014-10-25 15:56:24.689 U Eat[79938:2166115] 99
2014-10-25 15:56:24.690 U Eat[79938:2166115] 99
2014-10-25 15:56:24.691 U Eat[79938:2166115] 99
2014-10-25 15:56:24.692 U Eat[79938:2166115] 99
2014-10-25 15:56:24.693 U Eat[79938:2166115] 99
2014-10-25 15:56:24.829 U Eat[79938:2166115] 99
2014-10-25 15:56:24.835 U Eat[79938:2166115] 99
2014-10-25 15:56:24.838 U Eat[79938:2166115] 99
2014-10-25 15:56:24.840 U Eat[79938:2166115] 99

enter image description here

1 个答案:

答案 0 :(得分:2)

问题在于您记录dayNumber的方式:因为它是NSObject,您应该像这样记录:

NSLog(@"Row=%d, Day=%@", indexPath.row, dayNumber);

请注意,该行记录为%d的整数,而dayNumber则记录为%@的对象。