UITable View numberOfRowsInSection& cellForRowAtIndexPath错误

时间:2012-12-01 16:10:01

标签: objective-c arrays uitableview

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

1 个答案:

答案 0 :(得分:0)

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