我的问题是我有一个 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;
}