开发者

UITAbleView Giving Error

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 //NSLog(@"Array: %@",rows);
    return [rows count];// AT THIS LINE
}

Program received signal: “EXC_BAD_ACCESS”

THANKS FOR THE REPLY Actually I have attached it to the WebPage By NSUrl where I have made a PHP array and I have created a NSLOG where I am getting the Values in the array form but When It exceute the line return [rows count];. It gives error when I am writting statically return 2; then it execute. I am explaining to you what I am doing. I am initialising the NIb with

Name tableViewController=[[JsonTestViewController alloc] initWithNibName:@"JsonTestViewController" bundle:nil];

In JsonTestViewController.m I have this code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSLog(@"Array: %@",rows);
    return [rows count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;   
    }

    // Configure the cell.
    NSDictionary *dict = [rows objectAtIndex: indexPath.row];
    NSString *strlb1=[dict objectForKey:@"block"];
    NSString *strlb2=[dict objectForKey:@"name"];
    strlb1=[strlb1 stringByAppendingString:@" , "];
    strlb1=[strlb1 stringByAppendingString:strlb2];
    NSString *str1=@"FPS : ";
    NSString *str2=[dict objectForKey:@"p_hours"];  
    NSString *strpinf;
    if([str2 isEqualToString:@"FP"])
    {
        strpinf=@"Free Parking";
    }
    else if([str2 isEqualToString:@"12"]) 
    {
        strpinf=@"2 hours";
    }
    else if([str2 isEqualToString:@"14"]) 
    {
        strpinf=@"4 hours";
    }
    else if([str2 isEqualToString:@"MP"]) 
    {
        strpinf=@"Metered Parking";
    }
    str1=[str1 stringByAppendingString:s开发者_Python百科trpinf];
    cell.textLabel.text =strlb1;
    cell.detailTextLabel.text = str1;



    return cell;
}
- (void)viewDidLoad {

    [super viewDidLoad];
    NSURL *url = [NSURL URLWithString:@"SITE URL"];
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
    NSError *error = nil;


    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    {
        rows = [dict objectForKey:@"users"];
    }

    NSLog(@"Array: %@",rows);
    [jsonreturn release];
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end


can you give more info? This can be anything, but most likely, rows is pointing to memory where a valid array used to be. How did you create the rows array?

For example, your rows array or dictionary not longer pointing to valid memory if you created the rows array as an autoreleased object through a factory method in another method.

Here's another question that's pretty close to what you're describing:

EXC_BAD_ACCESS signal received

EDIT:

So looking at the code you provided, with these lines there are some possibilities:

NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error]; 
    if (dict) { rows = [dict objectForKey:@"users"]; }

the deserializeAsDictionary method can return either an autoreleased dictionary or NULL. so one possibility is that rows = NULL. when you try [rows count], your program will crash. Check and see what's in error, might give you some clues.

This will cause an error even when you explicitly return 2 for numberOfRowsInSection: because in cellForRowAtIndexPath:, you're still trying to access rows, even if it could possibly be NULL.

the other possibility lies in how you've defined rows. I'm guessing it's a property in your class. But where you have rows=[dict objectForKey:@"users"];, rows can point to nothing after the method's finished. Rows will still have the address of where [dict objectForKey:] was, but after the scope of the method, dict may be gone and all the data that comes with it.

NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];

under the KVC guidelines, you should expect dict to autorelease after the end of method.

and another possibility is, since i don't know the specifics of the JSON class you're using, is that when you release jsonreturn, you're also dealloc'ing all the data associated with it. So in effect, rows is pointing to nothing.

case in point, the error seems to be rooted in how you're setting/retaining/accessing rows.

try using the Build->Build&Analyze in xcode. it might give you some more hints. or throw in a bunch of NSLog(@"%d",[rows count]); all over. also try using the debugger. it'll give you a trace of method calls that lead up to [rows count] faulting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜