Objective c 当i'时获取EXC#u BAD#u访问权限;m在UITableView中选择行
我在选择表中的行时遇到问题 当我选择任何一行或尝试滚动表格时,我获得了EXC\u BAD\u访问权限 我的观点很简单。 用户按下on按钮后,我创建了一个包含10个对象的表(Objective c 当i'时获取EXC#u BAD#u访问权限;m在UITableView中选择行,objective-c,uitableview,Objective C,Uitableview,我在选择表中的行时遇到问题 当我选择任何一行或尝试滚动表格时,我获得了EXC\u BAD\u访问权限 我的观点很简单。 用户按下on按钮后,我创建了一个包含10个对象的表(Foodclass)Food是一个带有id和名称的简单类。我有一个NSMutableArray,它包含了所有对象 谢谢你的帮助 守则: @interface SearchFood : UIViewController <UITextFieldDelegate, UITableViewDataSource, U
Food
class)Food
是一个带有id和名称的简单类。我有一个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
@interface SearchFood:UIViewController
{
UITableView*食物表;
}
@属性(非原子,保留)NSMutableArray*FoodsArray;
-(无效)搜索按钮单击:(id)发件人;
@结束
*************************************
进口“SearchFood.h”
导入“AppDelegate.h”
进口“Food.h”
@食品的实施
-(无效)viewDidLoad
{
[超级视图下载];
self.title=@“搜索食物”;
self.view.backgroundColor=[UIColor whiteColor];
BTN搜索=[UIButton Button,类型为:UIButtonTypedTailDisclosure];
[btnSearch setFrame:CGRectMake(2201010040)];
[BTN搜索集标题:@“Serach”用于状态:UIControlStateNormal];
[BTN搜索添加目标:自我操作:@选择器(搜索按钮单击:)用于控制事件:UIControlEventTouchDown];
[self.view addSubview:btnSearch];
[BTN搜索发布];
食品*新食品=[新食品];
newfood.Food_Name=@“guy”;
self.FoodsArray=[NSMutableArray数组,其对象为:newfood,nil];
}
-(无效)搜索按钮单击:(id)发件人
{
FoodTable=[[UITableView alloc]initWithFrame:CGRectMake(0,80320460)
样式:UITableViewStylePlain];
foodsTable.dataSource=self;
foodsTable.delegate=self;
FoodTable.AllowSelection=是;
[self.view addSubview:foodsTable];
[食物表释放];
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[self.FoodsArray count];
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
NSLog(“按行”);
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*cellIdentifier=@;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellIdentifier]自动释放];
如果(!(indexath.row>=[self.FoodsArray count]))
{
Food*currFood=(Food*)[self.FoodsArray objectAtIndex:indexPath.row];
cell.textlab.text=currFood.Food\u Name;
[单元格设置选择样式:UITableViewCellSelectionStyle蓝色];
}
返回单元;
}
*********************
食物类别:
@接口食物:NSObject
@属性(非原子,保留)NSString*食物ID;
@属性(非原子,保留)NSString*食品名称;
@结束
#进口“Food.h”
@实施食品
@合成食物素;
@合成食品名称;
@结束
如果单元格不是第一次启动,dequeueReusableCellWithIdentifier:
返回nil
。因此,您不能使用不存在的单元格。我不确定您的意思。我正在对牢房做“initWithStyle”。你能说得更具体一点吗?我想,我以前看过一些不同的东西,你编辑过吗?现在,您似乎扔掉了出列单元,但这不应该引起您的问题。你能把食品类的代码贴出来吗?可能异常处的调用跟踪也会有所帮助。请尝试UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];if(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];}no=\im选择任何行时出现相同错误。这门课很简单。2属性:属性{非原子,保留}NSString*食物id;属性{非原子,保留}NSString*食物名称;您提到了10个对象,在您的代码中只有一个。填充数组时会出现问题吗?您的代码有很多问题!请阅读一些介绍材料。例如,您的命名会导致无法读取的代码。然后,您将对单元格进行排队,并在下一行创建一个新单元格。为什么?这没有任何意义。这是!(indexath.row>=[self.FoodsArray count])
肯定不是您想要做的。您认为[Food new]
在做什么?当然,他的代码肯定有问题。但是!(indexPath.row>=[self.FoodsArray count])
相当于绝对正确的(indexPath.row<[self.FoodsArray count])
,而[Food new]
是[[Food alloc]init]的缩写。
您能发布堆栈跟踪吗?当崩溃发生时,Xcode中的控制台应该准确地记录出什么地方出了问题。如果你能贴出来,我们可以帮你。