在Tableview中显示NSMutableArray数据时出现问题

时间:2011-04-10 22:16:13

标签: iphone objective-c xcode

HI,我是iphone编程新手,我使用XML Parser接收数据,我将数据存储在NSMutable Array中。 (我可以使用NSLog在控制台中查看数据)我无法在表视图中显示数据。你能帮我吗?这是我的代码。

#import <UIKit/UIKit.h>
#import "NamazTime.h"
#import <Foundation/NSXMLParser.h>
#import "NamazTimeController.h"

@protocol NSXMLParserDelegate;


@interface NamazTimeController : UITableViewController <NSXMLParserDelegate> {

    NSMutableArray *colorNames;

}
@property (nonatomic, retain) NSMutableArray *colorNames;
- (void)loadNamTime;

@end




//
//  NamazTimeController.m
//  MCVNamaz
//
//  Created by Andy on 28/03/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NamazTimeController.h"
#import "WSNamaz.h"
#import "NamazTime.h"

@implementation NamazTimeController
@synthesize colorNames;



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

//NamazTime *namTime = [[NamazTime alloc] init];



- (id)init {
    //Call the superclass's designated initializer
    [super initWithNibName:nil bundle:nil];
    //Get the tab bar item
    UITabBarItem *tbi = [self tabBarItem];
    //Give it a label
    [tbi setTitle: @"Today"];
    UIImage *i = [UIImage imageNamed:@"Time.png"];
    [tbi setImage:i];


//  [arrWeek removeAllObjects];

    [[self tableView] reloadData];
    return self;
}

#pragma mark -
#pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];    

    NSLog(@"view will appear works!!!!!");


    [super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
//    return <#number of sections#>;
}

*/

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.colorNames count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    /*
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"UITableViewCell"] autorelease];
    }
//  [[cell textLabel] setText:[arrWeek objectAtIndex:[indexPath row]]];
    return cell;
     */
    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell.
    cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];

    return cell;


}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

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

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

- (void)viewDidLoad{
    NSLog(@"array view load called");
//  self.colorNames = [[NSMutableArray alloc] initWithObjects: @"colorNames", nil];
    [super viewDidLoad];
//  NamazTime *nt = [[NamazTime alloc] init];
//  self.colorNames = [nt loadNamTime];

//  WSNamaz *wsnamaz = [[WSNamaz alloc] init];
//  self.colorNames = wsnamaz ver
//  [wsnamaz verifyXML];
    //NamazTimeController *ntcc = [[NamazTimeController alloc] init];
//  self.colorNames = wsnamaz.namazXmlArray;

    //self.colorNames = [[NSMutableArray alloc] initWithObjects:colorNames, nil];
    self.colorNames = [[NSMutableArray alloc] init];
    //NamazTime *nt = [[NamazTime alloc] init];
    //self.colorNames = [nt loadNamTime];

    //NamazTimeController *wtcObject = [[NamazTimeController alloc] init];
    //[wsObject verifyXML];

    //self.colorNames = [[NSMutableArray alloc] init];

    //self.colorNames = [nt loadNamTime];
//  [wsnamaz verifyXML];
    //  displayText.text = @"text";
}

- (void)viewDidUnload {
    self.colorNames = nil;

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)loadNamTime
{
    NamazTime *nt = [[NamazTime alloc] init];
    self.colorNames = [nt loadNamTime];
    for (int i = 0; i < 5; i++)
        NSLog (@"Element of color names  in the namaz time controller %i = %@", i, [colorNames objectAtIndex: i]);

    //[[self tableView] reloadData];
    [self viewDidLoad];


}   


@end

1 个答案:

答案 0 :(得分:1)

这里有很多问题。希望这可以帮助您至少让您的应用运行:

  1. 在stackoverflow上获得更好响应的一般注释:清理代码,只粘贴与您提出的问题相关的部分,并正确格式化。该示例未被格式化为代码块,并且包含如此多的注释部分,使您难以提供帮助。

  2. init未正确初始化实例。首先,您需要使用self = [super initWithNibName:nil bundle:nil]。其次,我会将return self中的所有内容包装在if (self) {}中,以便只有在初始化成功时才会执行。

  3. 同样在init中,initXXX方法中未加载视图,因此您应删除[[self tableView] reloadData]

  4. viewWillAppear中,您要拨打[super viewWillAppear]两次。可能没有害处,但绝对没有必要。由于您在此方法中不执行任何操作,因此无论如何我都会完全删除它。

  5. 关于将数据加载到表中,您不需要在第一次加载视图时调用reloadData b / c tableView将在第一次显示时加载自身。每当数据模型更改需要时,您都需要调用[tableView reloadData]。在您的情况下,您需要尝试在loadNamTime中执行此操作。您不应该调用[self viewDidLoad],因为这是不正确的,可能会产生不必要的副作用。通常,您不应该手动调用viewDidLoad,这是基于实际加载的视图的系统消息。取消注释行[[self tableView] reloadData]