简体   繁体   中英

App crashes when button is pressed that points to table view

I've got a button pointing to a view and whenever I see press the button my app crashes. Can anyone tell me why? The code is the implementation file for the view that the button pushes to.

@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
- (void)viewDidLoad

{

[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];


myMutableArray = [[NSMutableArray alloc]init];
[self storingObjectsFromDictionaryToArray];
// Do any additional setup after loading the view, typically from a nib.

}

-(void)storingObjectsFromDictionaryToArray

{

NSArray *arr = [myDictionary allKeys];


  [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:0]]];
NSString *str = [NSString stringWithFormat:@"%@",[myDictionary valueForKey:[arr objectAtIndex:1]]];
[myMutableArray addObject:str];
str = [NSString stringWithFormat:@"%@",[myDictionary valueForKey:[arr objectAtIndex:2]]];
[myMutableArray addObject:str];
 [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:3]]];
 [myMutableArray addObject:[myDictionary valueForKey:[arr objectAtIndex:4]]];
 [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:5]]];
 [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:6]]];
 [myMutableArray addObject:[myDictionary valueForKey:[arr objectAtIndex:7]]];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

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;

}

The error is Thread 1: breakpoint 9.1

Ahm ... you might have a breakpoint activated ?

You are not getting an error - it is just cause of you having an activated breakpoint.

If you're using Xcode 4, then you can see 'Breakpoints' as seen below. Just click on that to disable breakpoints.

在此处输入图片说明

If you are using Xcode 5, then you have to click this Blue button to disable the breakpoint.

在此处输入图片说明

As AlexVogel pointed out in the comments, use init method to initialize the dictionary. Alternatively, you can use lazy loading using property:

- (NSDictionary *)myLoadedDictionary{
    if (!myDictionary) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        myDictionary = [defaults dictionaryRepresentation];
    }
    return myDictionary;
}

Now, in your class, consume [self myLoadedDictionary] instead of myDictionary.

I am not on my machine, but I am pretty much sure that you can define even myDictionary as a getter so that you can use that same name in your code and not myLoadedDictionary.

Like everyone else I would like to point out that should post your error.

Now I also see one problem with cellAtindexPath logic. Your trying to set data on null cell, see [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; will only give you something back if you've already given it something.

You've two options for loading a cell, the new way or the old way, both are kinda of same.

Option one and I recommend this one:

in your viewDidLoad (or what not) register a class for a cell identifer:

[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

In CellForIndexPath to get the cell go:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

The forIndexPath is important here, using old api will return a nil cell.

Option 2:

Load a cell and if its nil, create a it.

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM