UITableview有EXT_BAD_ACCESS然后向下滚动

时间:2011-04-08 06:07:02

标签: xcode parsing uitableview

我在.h文件中有以下代码

#import <UIKit/UIKit.h>


@interface MessDisViewController : UIViewController {
IBOutlet UITableView * DisTable;
NSMutableArray *massages;
UIActivityIndicatorView * activityIndicator;
CGSize cellSize;
NSXMLParser * friendsParser;
NSMutableDictionary * listOfFriends;
NSString * currentElement;
NSMutableString * currentID, * currentFname;
}
-(void) parseXMLFileAtURL:(NSString *) myUrl;
-(UITableViewCell *) getCellContentView:(NSString *)cellIdentifier;
@end

在.m文件中我有

#import "MessDisViewController.h"
#import "MessageDetailView.h"

@implementation MessDisViewController


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}   

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [massages count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
    cell = [self getCellContentView:CellIdentifier];

UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UILabel *lblTemp2 = (UILabel *)[cell viewWithTag:2];

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

NSString *cellValue = [massages objectAtIndex:indexPath.row];
NSString *title = [[[massages objectAtIndex: storyIndex] objectForKey: @"theme"] stringByReplacingOccurrencesOfString:@"*" withString:@" "];
NSLog(@"%@",title);
lblTemp1.text = title;
lblTemp2.text= [NSString stringWithFormat:@"%@, %@",[[massages objectAtIndex: storyIndex] objectForKey: @"login"],[[massages objectAtIndex: storyIndex] objectForKey: @"activate_date"]];
[cellValue release];
return cell;

}

- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {

CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(10, 33, 290, 25);
UILabel *lblTemp;

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];

//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];

//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.tag = 2;
lblTemp.font = [UIFont boldSystemFontOfSize:12];
lblTemp.textColor = [UIColor lightGrayColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];

return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return 60;
}
- (void) parseXMLFileAtURL:(NSString *) URL{    
massages = [[NSMutableArray alloc] init];

NSURL *xmlURL = [NSURL URLWithString:URL];  
friendsParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[friendsParser setDelegate:self];
[friendsParser setShouldProcessNamespaces:NO];
[friendsParser setShouldReportNamespacePrefixes:NO];
[friendsParser setShouldResolveExternalEntities:NO];
[friendsParser parse];

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Cannot connect to DataBase  (Error code %i )", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);

UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{            
if ([elementName isEqualToString:@"Message"]){
    listOfFriends = [[NSMutableDictionary alloc] init];
    [listOfFriends setObject:[attributeDict objectForKey:@"theme"] forKey:@"theme"];
    [listOfFriends setObject:[attributeDict objectForKey:@"login"] forKey:@"login"];
    [listOfFriends setObject:[attributeDict objectForKey:@"activate_date"] forKey:@"activate_date"];
    [listOfFriends setObject:[attributeDict objectForKey:@"message_id"] forKey:@"message_id"];
    [massages addObject:[listOfFriends copy]];
}
}   

- (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
NSString *request=[NSString stringWithFormat: @"http://blablabla"];
[self parseXMLFileAtURL:request];

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *mesid = [[NSString alloc] initWithString:[[massages objectAtIndex: storyIndex] objectForKey: @"message_id"]];
NSLog(@"%@",mesid);
MessageDetailView *mesdet = [[MessageDetailView alloc]initWithNibName:nil bundle:nil];
mesdet.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
mesdet.mesid=mesid;
[self presentModalViewController:mesdet animated:YES];

[mesdet release];
mesdet = nil;
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];

NSLog(@"all done!");
NSLog(@"stories array has %d items", [massages count]);
[DisTable reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}



- (void)dealloc {


[friendsParser release];
[listOfFriends release];    
[super dealloc];
}

@end

当我在模拟器上向下滚动我的tableview时,它有EXT_BAD_ACCESS为什么会发生以及如何解决这个问题

2 个答案:

答案 0 :(得分:0)

启用NSZombie并首先测试创建错误的内容

Double-click an executable in the Executables group of your Xcode project.
Click the Arguments tab.
In the "Variables to be set in the environment:" section, make a variable called "NSZombieEnabled" and set its value to "YES". 
祝你好运

答案 1 :(得分:0)

解决此问题

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];


NSString *title = [[[massages objectAtIndex: storyIndex] objectForKey: @"theme"] stringByReplacingOccurrencesOfString:@"*" withString:@" "];
NSLog(@"%@",title);
lblTemp1.text = title;
lblTemp2.text= [NSString stringWithFormat:@"%@, %@",[[massages objectAtIndex: storyIndex] objectForKey: @"login"],[[massages objectAtIndex: storyIndex] objectForKey: @"activate_date"]];
return cell;