2011-04-10 86 views
0

嗨,我是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

這裏有一堆問題。希望這將幫助你至少可以得到您的應用程序運行:

  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