Ios 当按下指向表格视图的按钮时,应用程序崩溃
我有一个按钮指向一个视图,每当我看到按下按钮,我的应用程序就会崩溃。谁能告诉我为什么?代码是按钮所按视图的实现文件Ios 当按下指向表格视图的按钮时,应用程序崩溃,ios,objective-c,uitableview,uibutton,nsuserdefaults,Ios,Objective C,Uitableview,Uibutton,Nsuserdefaults,我有一个按钮指向一个视图,每当我看到按下按钮,我的应用程序就会崩溃。谁能告诉我为什么?代码是按钮所按视图的实现文件 @implementation OpenShiftViewController -(void)viewDidLoad { [super viewDidLoad]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; myDictionary = [defaults dictio
@implementation OpenShiftViewController
-(void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myDictionary.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray * allKeys = [myDictionary allKeys];
cell.textLabel.text = [myDictionary objectForKey:[allKeys objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [myDictionary objectForKey:[allKeys objectAtIndex:indexPath.row]];
return cell;
}
@end
错误是线程1:断点9.1
啊。。。您可能激活了断点?您没有收到错误-这只是您激活断点的原因 如果您使用的是Xcode 4,那么您可以看到“断点”,如下所示。只需单击它即可禁用断点 如果您使用的是Xcode 5,则必须单击此蓝色按钮以禁用断点
正如AlexVogel在评论中指出的,使用init方法初始化字典。 或者,您可以使用以下属性使用延迟加载:
- (NSDictionary *)myLoadedDictionary{
if (!myDictionary) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];
}
return myDictionary;
}
现在,在您的类中,使用[self myLoadedDictionary]而不是myDictionary
- (void)viewDidLoad
我不在我的机器上,但我非常确定您甚至可以将myDictionary定义为getter,这样您就可以在代码中使用相同的名称,而不是myLoadedDictionary
- (void)viewDidLoad
{
}
{
}
{
}
{
}
{
}像其他人一样,我想指出的是,应该发布您的错误 现在我也看到了cellAtindexPath逻辑的一个问题。您试图在空单元格上设置数据,请参阅[tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果你已经给了它一些东西,它才会还给你一些东西 加载单元格有两种选择,新方法和旧方法,两者都差不多 选项一我建议: 在viewDidLoad(或其他)中,为单元格标识符注册一个类:
[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
在CellForIndexPath中,要启动单元格,请执行以下操作:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
forIndexPath在这里很重要,使用旧api将返回一个nil单元格
选项2:
加载单元格,如果为零,则创建一个新的单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
错误信息是什么?按钮的iAction是什么?“指向视图”是什么意思?请尝试在init方法中加载“myDictionary”,因为我认为UITableViewDelegate/Datasource方法在正确初始化“myDictionary”之前正在请求它。按钮的iAction是(iAction)openShift:(id)sender;错误是线程1:断点9.1如何停用它?我正在使用xcode 5并已停用断点,但它仍然崩溃。。那是个例外。。你知道“[defaults dictionaryRepresentation]”返回什么吗?它返回一个数组字典。“cell.textLabel.text=[myDictionary objectForKey:[allKeys objectAtIndex:indexPath.row]];”在这一行中,您必须为cell.textLabel.text提供字符串类型的数据,但要提供数组。。所以你得到了dat异常。在下面的回答中,我发布了正确的代码,只是在datI中查看一下,我已经完成了这项工作,它工作得很好,所以谢谢你。但是,在表视图中,它会为国家(如英国)加载大量字母和其他垃圾。我只希望有尽可能多的行,因为有保存的班次。谢谢
return 1;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return myMutableArray.count;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myMutableArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [myMutableArray objectAtIndex:indexPath.row];
return cell;
[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}