【问题标题】:Problem in displaying NSMutableArray data in the Tableview在 Tableview 中显示 NSMutableArray 数据时出现问题
【发布时间】:2011-04-10 22:16:13
【问题描述】:

嗨,我是 iphone 编程新手,我正在使用 XML Parser 接收数据,我将数据存储在 NSMutable 数组中。 (我可以使用 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

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案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,因为 tableView 将在第一次显示时自行加载。每当数据模型更改需要时,您都需要致电[tableView reloadData]。在您的情况下,您需要尝试在loadNamTime 中执行此操作。你不应该打电话给[self viewDidLoad],因为这是不正确的并且可能会产生不必要的副作用。通常,您永远不应该手动调用viewDidLoad,这是基于实际加载的视图的系统消息。取消注释 [[self tableView] reloadData] 行。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    相关资源
    最近更新 更多