在表格视图中显示NSMutableArray数据的问题

问题描述:

嗨,我是iphone编程的新手,我使用XML解析器接收数据,我将数据存储在NSMutable数组中。 (我可以看到使用NSLog在控制台中的数据)我不能在表视图中显示数据。你能帮我吗?这是我的代码。在表格视图中显示NSMutableArray数据的问题

#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. 一般性意见,以获得更好的响应上计算器:清理你的代码,只粘贴有关你问这个问题的部分,并正确格式化。该示例没有被格式化为代码块,并且包含很多注释掉的部分,这些部分让我们很难提供帮助。

  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]

+0

+1花费的时间;) – Jordan 2011-04-10 23:05:19

+0

嗨,我已经放置了一个tableview查询。你可以在下面的链接上检查并回答这个问题。 http://stackoverflow.com/questions/8239543/tableview-is-not-showing-data – 2011-12-03 07:21:26