Получение EXC_BAD_ACCESS, когда я выбираю строку в UITableView - PullRequest
0 голосов
/ 24 марта 2012

У меня проблема при выборе строк в моей таблице.

Когда я выбираю любую строку или пытаюсь прокрутить таблицу, я получаю EXC_BAD_ACCESS.

Мой взгляд очень прост.После того, как пользователь нажал на кнопку, я создаю таблицу из 10 объектов (класс Food).Food - это простой класс с идентификатором и именем.У меня есть NSMutableArray, который охватывает все объекты.

Спасибо за вашу помощь!

Код:

@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

1 Ответ

0 голосов
/ 24 марта 2012

Если ячейка не инициируется в первый раз, dequeueReusableCellWithIdentifier: возвращает nil.Таким образом, вы не можете использовать несуществующую ячейку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...