滚动uitableview时执行错误访问权限

时间:2013-09-12 10:55:22

标签: iphone ios uiviewcontroller uitableview

我正在尝试在tableview中填充XML数据列表。有一个用于保存tableview的Uiviewcontroller,一个用于下载和解析数据的xmlparser以及一个包含XML数据属性的类。

UIViewcontroller.h

    #import <UIKit/UIKit.h>
    #import "XMLParser.h"
    @interface TableViewTutViewController : UIViewController <UITableViewDelegate,  UITableViewDataSource> {

    IBOutlet UITableView *myTableView;
    XMLParser *xmlParser;
    EnquiryData *currentEnquiry;

    }
    @property (nonatomic, retain) IBOutlet UITableView *myTableView;
    @property(nonatomic, retain) XMLParser *xmlParser;
    @property(nonatomic, retain) EnquiryData *currentEnquiry;
@end

UIViewcontroller.m

    #import "TableViewTutViewController.h"
    #import "EnquiryViewController.h"
    #import "AppDelegate.h"

    @implementation TableViewTutViewController
    @synthesize xmlParser;
    @synthesize myTableView;

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {

    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }
    -(void)viewDidLoad
    {

    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];    
    NSString *welcome= @"Welcome ";
    welcome= [welcome stringByAppendingString:app.firstname];    
    NSString *url = @"http://demos4clients.com/iphone/csi/register.php?choice=getEnquiryList&id_user=";
    url = [url stringByAppendingString:app.id_user];
     xmlParser = [[XMLParser alloc] loadXMLByURL:url];    
    [super viewDidLoad];

    }    

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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section     {
    if(section == 0 || section == 1)
        return 1;
        else{
        NSLog(@"%i", [[self.xmlParser datas]count]);
        int i = (int)[[self.xmlParser datas] count];
        i++;      
        return i;

    }
}    

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
               cell.textLabel.highlightedTextColor = [UIColor yellowColor];                 


    }    
    if(indexPath.section == 0){
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20];
    cell.textLabel.text = @"Add Enquiry";
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    cell.selectionStyle = UITableViewCellAccessoryNone;
    }
    if(indexPath.section == 1)
    {
        if(indexPath.row == 0)
        {
            cell.textLabel.text = @"Enquiry";
            UIImage *accept= [UIImage imageNamed:@"accept_ico.png"];
            UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeCustom];
            acceptButton.frame = CGRectMake(110, 10, 20, 21);
            [acceptButton.layer setBorderWidth:0];
            [acceptButton setBackgroundImage:accept forState:UIControlStateNormal];
            [acceptButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];            
            [cell.contentView addSubview:acceptButton];
            [acceptButton release];
            [accept release];
            UIImage *view= [UIImage imageNamed:@"view_ico.png"];
            UIButton *viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
            viewButton.frame = CGRectMake(190, 10, 20, 21);
            [viewButton.layer setBorderWidth:0];
            [viewButton setBackgroundImage:view forState:UIControlStateNormal];
            [viewButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:viewButton];
            [viewButton release];
            [view release];
            UIImage *new= [UIImage imageNamed:@"new_ico.png"];
            UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
            newButton.frame = CGRectMake(270, 10, 20, 21);
            [newButton.layer setBorderWidth:0];
            [newButton setBackgroundImage:new forState:UIControlStateNormal];
            [newButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:newButton];
            [new release];
            [newButton release];                

        }

    }
    if(indexPath.section == 2){     

        currentEnquiry = [[xmlParser datas] objectAtIndex:indexPath.row];
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:20];
        cell.textLabel.text = currentEnquiry.makeCar;        
        [currentEnquiry release];
        UILabel  * acceptLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 8, 30, 30)];
        acceptLabel.backgroundColor = [UIColor clearColor];
        acceptLabel.textAlignment = UITextAlignmentCenter; // UITextAlignmentCenter, UITextAlignmentLeft
        acceptLabel.textColor=[UIColor blueColor];
        acceptLabel.numberOfLines=0;
        acceptLabel.lineBreakMode=UILineBreakModeWordWrap;
        acceptLabel.text = @"12";
        [cell.contentView addSubview:acceptLabel];
        [acceptLabel release];

        UILabel  * viewLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 8, 30, 30)];
        viewLabel.backgroundColor = [UIColor clearColor];
        viewLabel.textAlignment = UITextAlignmentCenter; // UITextAlignmentCenter, UITextAlignmentLeft
        viewLabel.textColor=[UIColor blueColor];
        viewLabel.numberOfLines=0;
        viewLabel.lineBreakMode=UILineBreakModeWordWrap;
        viewLabel.text = @"1";
        [cell.contentView addSubview:viewLabel];
        [viewLabel release];


        UILabel  * newLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 8, 30, 30)];
        newLabel.backgroundColor = [UIColor clearColor];
        newLabel.textAlignment = UITextAlignmentCenter; // UITextAlignmentCenter, UITextAlignmentLeft
        newLabel.textColor=[UIColor blueColor];
        newLabel.numberOfLines=0;
        newLabel.lineBreakMode=UILineBreakModeWordWrap;
        newLabel.text = @"5";
        [cell.contentView addSubview:newLabel];
        [newLabel release];


    }

    return cell;
    }


   @end

XML.h

    #import <Foundation/Foundation.h>

    @interface EnquiryData : NSObject
    {
    NSString *makeCar;
    NSString *modelCar;
    NSString *yearCar;
    NSString *minPrice;
    NSString *maxPrice;
    NSString *minRun;
    NSString *maxRun;
    NSString *location;
    NSString *fuelType;
    NSString *searchRadius;

    }
    @property (nonatomic, retain) NSString   *makeCar;
    @property (nonatomic, retain) NSString   *modelCar;
    @property (nonatomic, retain) NSString   *yearCar;
    @property (nonatomic, retain) NSString   *minPrice;
    @property (nonatomic, retain) NSString   *maxPrice;
    @property (nonatomic, retain) NSString   *minRun;
    @property (nonatomic, retain) NSString   *maxRun;
    @property (nonatomic, retain) NSString   *location;
    @property (nonatomic, retain) NSString   *fuelType;
    @property (nonatomic, retain) NSString   *searchRadius;

   @end

XML..m文件

    #import "EnquiryData.h"

    @implementation EnquiryData
    @synthesize makeCar;
    @synthesize modelCar;
    @synthesize yearCar;
    @synthesize minPrice;
    @synthesize maxPrice;
    @synthesize minRun;
    @synthesize maxRun;
    @synthesize searchRadius;
    @synthesize location;
    @synthesize fuelType;


    @end

解析器(.h)文件

    #import <Foundation/Foundation.h>
    #import "EnquiryData.h"
    @interface XMLParser : NSObject<NSXMLParserDelegate>
    {
    NSMutableString *currentNodeContent;
    NSMutableArray  *datas;
    NSXMLParser     *parser;
    EnquiryData     *recentEnquiry;
    }
    @property (readonly, retain) NSMutableArray *datas;

    -(id) loadXMLByURL:(NSString *)urlString;
    @end

parser.m

    #import "XMLParser.h"

    @implementation XMLParser
    @synthesize datas;


    -(id) loadXMLByURL:(NSString *)urlString
    {
    datas          = [[NSMutableArray alloc] init];
    NSURL *url      = [NSURL URLWithString:urlString];
    NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
    parser          = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;
    }
    - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
    if ([elementname isEqualToString:@"enquiry"])
    {
            recentEnquiry = [EnquiryData alloc];
    }
    }

    - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {

    if ([elementname isEqualToString:@"make"])
    {
        recentEnquiry.makeCar = currentNodeContent;
    }
    if ([elementname isEqualToString:@"model"])
    {
        recentEnquiry.modelCar = currentNodeContent;
    }
    if ([elementname isEqualToString:@"model_yr"])
    {
        recentEnquiry.yearCar = currentNodeContent;
    }
    if ([elementname isEqualToString:@"min_price"])
    {
        recentEnquiry.minPrice = currentNodeContent;
    }
    if ([elementname isEqualToString:@"max_price"])
    {
        recentEnquiry.maxPrice = currentNodeContent;
    }
    if ([elementname isEqualToString:@"min_run"])
    {
        recentEnquiry.minRun = currentNodeContent;
    }
    if ([elementname isEqualToString:@"max_run"])
    {
        recentEnquiry.maxRun = currentNodeContent;
    }
    if ([elementname isEqualToString:@"fuel_type"])
    {
        recentEnquiry.fuelType = currentNodeContent;
    }
    if ([elementname isEqualToString:@"location"])
    {
        recentEnquiry.location = currentNodeContent;
    }
    if ([elementname isEqualToString:@"search_radius"])
    {
        recentEnquiry.searchRadius = currentNodeContent;
        [datas addObject:recentEnquiry];
    }

    }

    - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    @end

2 个答案:

答案 0 :(得分:1)

您已使用不带分配的发布声明

什么时候为对象分配内存然后只用于该对象的release语句

对于Ex:UIButton *btn = [UIButton alloc]init];

然后释放按钮使用结束[btn release];

一旦Crass检查此声明[currentEnquiry release];

答案 1 :(得分:0)

在XMLParser .m文件中实现此解析器委托方法:

    - (void)parserDidEndDocument:(NSXMLParser *)parser  
 {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadTable" object:nil];
 }

在ViewController.m viewDidLoad方法中将numberOfRows整数变量设置为零

-(void)viewDidLoad
{

numberOfRows = 0;

AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];    
NSString *welcome= @"Welcome ";
welcome= [welcome stringByAppendingString:app.firstname];    
NSString *url = @"http://demos4clients.com/iphone/csi/register.php?choice=getEnquiryList&id_user=";
url = [url stringByAppendingString:app.id_user];
 xmlParser = [[XMLParser alloc] loadXMLByURL:url];   


[super viewDidLoad];

}

在ViewController.m viewWillAppear方法中实现此通知的Observer,即

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable:) name:@"ReloadTable" object:nil];

}




- (void)reloadTable: (NSNotification *)notif
 {
    numberOfRows = [[self.xmlParser datas]count];
    [table reloadData];
 }

在ViewController.m viewWillDisappear方法中实现删除此通知的Observer,即

 - (void)viewWillDisappear:(BOOL)animated{

     [[NSNotificationCenter defaultCenter] removeObserver:self];
}

在UITableView数据源中行数的方法部分以这种方式实现:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
     {

            if(numberOfRows == 0)
            return 0;

            return (section == 0 || section == 1) ? 1 : numberOfRows + 1;

}

我相信你不会遇到exe访问不良问题