테이블의 선택 행 중 문제가 발생했습니다.UITableView에서 행을 선택할 때 EXC_BAD_ACCESS 가져 오기
행을 선택하거나 테이블을 스크롤하려고하면 EXC_BAD_ACCESS가 표시됩니다.
내보기는 매우 간단합니다. 사용자가 버튼을 누르면 10 개의 객체 (Food
클래스)가있는 표가 생성됩니다. Food
은 id와 name을 가진 단순한 클래스입니다. 나는 모든 obejcts를 conatians 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
코드에 많은 문제가 있습니다! 소개 자료를 읽어보십시오. 예를 들어, 이름을 지정하면 읽을 수없는 코드가됩니다. 그런 다음 셀을 큐에서 제거하고 다음 줄에서 새 셀을 만듭니다. 왜? 이것은 의미가 없습니다. 그리고이'! (indexPath.row> = [self.FoodsArray count])'는 의도 한 것이 아니 었습니다. – dasdom
'[새 음식]'은 무엇을하고 있다고 생각하십니까? – dasdom
글쎄, 그의 코드에는 문제가있다. 그러나'! (indexPath.row> = [self.FoodsArray count])'는 절대적으로 정확한'(indexPath.row <[self.FoodsArray count])'와 동일하고'[Food new]'는' [[Food alloc] init] ' – Matthias