Ich habe ein Problem während der Auswahlzeilen in meiner Tabelle.EXC_BAD_ACCESS abrufen, wenn ich eine Zeile in UITableView auswähle
Wenn ich irgendeine Zeile auswähle oder versuche, durch die Tabelle zu scrollen, bekomme ich EXC_BAD_ACCESS.
Meine Ansicht ist sehr einfach. Nachdem der Benutzer die Taste gedrückt hat, erstelle ich eine Tabelle mit 10 Objekten (Food
Klasse). Food
ist eine einfache Klasse mit ID und Name. Ich habe eine NSMutableArray
, die alle Objects enthält.
Danke für Ihre Hilfe!
Der Code:
@interface SearchFood : UIViewController <UITextFieldDelegate,
UITableViewDataSource, UITableViewDelegate>
{
UITableView * foodsTable;
}
@property(nonatomic, retain) NSMutableArray *FoodsArray;
-(void)searchButtonClick:(id)sender;
@end
*************************************
import "SearchFood.h"
import "AppDelegate.h"
import "Food.h"
@implementation SearchFood
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Search for food";
self.view.backgroundColor = [UIColor whiteColor];
btnSearch = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[btnSearch setFrame:CGRectMake(220, 10,100, 40)];
[btnSearch setTitle:@"Serach" forState:UIControlStateNormal];
[btnSearch addTarget:self action:@selector(searchButtonClick:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btnSearch];
[btnSearch release];
Food *newfood = [Food new];
newfood.Food_Name = @"guy";
self.FoodsArray = [NSMutableArray arrayWithObjects:newfood, nil];
}
-(void)searchButtonClick:(id)sender
{
foodsTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 80, 320, 460)
style:UITableViewStylePlain];
foodsTable.dataSource = self;
foodsTable.delegate = self;
foodsTable.allowsSelection = YES;
[self.view addSubview:foodsTable];
[foodsTable release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.FoodsArray count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath {
NSLog(@"Press row");
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"<#MyCell#>";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
if (!(indexPath.row >= [self.FoodsArray count]))
{
Food * currFood = (Food *)[self.FoodsArray objectAtIndex:indexPath.row];
cell.textLabel.text = currFood.Food_Name;
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
}
return cell;
}
*********************
Food class:
@interface Food : NSObject
@property (nonatomic, retain) NSString* Food_ID;
@property (nonatomic, retain) NSString * Food_Name;
@end
#import "Food.h"
@implementation Food
@synthesize Food_ID;
@synthesize Food_Name;
@end
Ihr Code hat viele Probleme! Bitte lies einleitende Sachen. Zum Beispiel führt Ihre Benennung zu unlesbarem Code. Dann entnimmt man die Zelle und in der nächsten Zeile erstellt man eine neue Zelle. Warum? Dies ergibt keinen Sinn. Und das '! (IndexPath.row> = [self.FoodsArray count])' ist sicher nicht das, was Sie vorhaben. – dasdom
Was glaubst du, was [Food new] 'macht? – dasdom
Nun, sicher hat sein Code Probleme. Aber '! (IndexPath.row> = [self.FoodsArray count])' ist äquivalent zu '(indexPath.row <[self.FoodsArray count])' was absolut korrekt ist, und '[Food new]' ist die Abkürzung für ' [[Food Alloc] init] ' – Matthias