0

我的问题是我有一个 UITable,我无法让 numberOfRowsInSection 与 cellForRowAtIndexPath 对应。

我的 foodCategoryObj 有三个数组,第一个数组有 6 个元素,第二个有 2 个,第三个有 3 个。

这一行 FoodCategory *foodCategoryObj = [appDelegate.foodCategoryArray objectAtIndex:indexPath.row];

..不断抛出这个错误..索引 2 超出范围 [0 .. 1]

总的来说,我不知道要为 numberOfRowsInSection 返回什么数字

arrayCountInt 还返回我在 viewWillAppear 中创建的数组中的对象数。

- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section {

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *arrayCountString = [prefs stringForKey:@"arrayCountString"];
NSInteger *arrayCountInt = [arrayCountString intValue];
//NSLog(@"arrayCountInt.. %i", (arrayCountInt) - 1);


AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSInteger *arrayCount = [appDelegate.foodCategoryArray count];


NSInteger *foodArray = [foodCatArray count];


// testing
NSLog(@"foodCatArray.. %i", foodArray);


return arrayCountInt;
}


- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
}

//NSLog(@"indexPath.row.. %i", (indexPath.row)-1);
FoodCategory *foodCategoryObj = [appDelegate.foodCategoryArray objectAtIndex:indexPath.row];

//Set the cell title
cell.text = foodCategoryObj.foodName;


// testing
//NSLog(@"%@", foodCategoryObj.foodName);
//NSLog(@"appDelegate.foodCategoryArray.. %@", appDelegate.foodCategoryArray);

return cell;
}
4

1 回答 1

0

你为什么不在[appDelegate.foodCategoryArray count]方法中返回numberofrowInSection。你应该返回你的foodArray并且你返回了错误的 var

于 2012-12-01T16:32:11.887 回答