关于iphone:在Tableview中显示NSMutableArray数据的问题

Problem in displaying NSMutableArray data in the Tableview

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#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

这里有很多问题。希望这将至少帮助您运行应用程序:

  • 一般注释,以获得对stackoverflow的更好响应:清理代码,仅粘贴与您所问问题相关的部分,并正确设置其格式。该示例未格式化为代码块,并且包含了很多注释掉的部分,这使我们难以提供帮助。

  • init没有正确初始化实例。首先,您需要使用self = [super initWithNibName:nil bundle:nil]。其次,我会将所有其他内容package在if (self) {}中的return self之前,以便仅在初始化成功后才执行。

  • 也在init中,视图未在initXXX方法中加载,因此您应删除[[self tableView] reloadData]

  • viewWillAppear中,您两次调用[super viewWillAppear]。可能不是有害的,但绝对没有必要。由于您在此方法中不执行任何操作,因此无论如何我都会将其完全删除。

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