-8

我正在尝试在我的tableview. 有一个保存 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

解析器

    #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
4

2 回答 2

1

您已使用未分配的发布声明

当您为对象分配内存时,仅用于该对象的释放语句

例如:UIButton *btn = [UIButton alloc]init];

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

[currentEnquiry release];一旦 Crass也检查这个语句

于 2013-09-12T11:27:49.020 回答
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 方法中为此通知实现观察者,即

-(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 方法中为此通知实现删除观察者,即

 - (void)viewWillDisappear:(BOOL)animated{

     [[NSNotificationCenter defaultCenter] removeObserver:self];
}

在 UITableView 数据源中的 Number Of Rows In Section 方法以这种方式实现:

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

            if(numberOfRows == 0)
            return 0;

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

}

我相信你不会遇到 exe bad access 问题

于 2013-09-12T13:13:42.173 回答