0
我的問題是,我有一個UITable,我無法得到numberOfRowsInSection與cellForRowAtIndexPath對應。UITable查看numberOfRowsInSection&cellForRowAtIndexPath錯誤
我foodCategoryObj具有三個陣列,第一陣列具有6個elems的,第二個有2和第三具有3.
此行 FoodCategory * foodCategoryObj = [appDelegate.foodCategoryArray objectAtIndex:indexPath.row];
..keeps引發此錯誤..索引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;
}
你的代碼似乎很困惑 - 你有一個數組在應用程序委託和另一個viewDidAppear?它看起來像你返回你在viewDidAppear中創建的數組的計數,但是在cellForRowAtIndexPath中,你從應用程序委託中的數組中獲取數據 - 這是一個問題。你的桌子上有沒有部分?如果是這樣,那麼你需要實現numberOfSectionsInTableView。如果你沒有部分,那麼你的數據不應該被組織成一個數組數組。 – rdelmar