我已經取得了一些進展,並且正在編輯問題以再次呈現。我現在最大的問題是分組表格視圖將會加載,但是每行都顯示每個部分的所有內容。此外,我的UIAlertView返回所有引用到我的plist(null)。我該如何解決這個問題,因爲對plist的引用正在殺死我。任何和所有的幫助將不勝感激!爲什麼我的plist不能填充我的UITableView?
這裏是我的plist文件的純文本版本:
<array>
<dict>
<key>eventName</key>
<string>Comedy Caravan</string>
<key>eventSpecifics</key>
<string>Nicholas Anthony</string>
<key>eventLocation</key>
<string>The Cats Den</string>
<key>eventType</key>
<string>Comedy</string>
<key>eventGoodies</key>
<string>Both</string>
<key>eventDate</key>
<date>2010-07-23T00:00:00Z</date>
</dict>
<dict>
<key>eventName</key>
<string>Comedy Caravan</string>
<key>eventLocation</key>
<string>The Cats Den</string>
<key>eventType</key>
<string>Comedy</string>
<key>eventSpecifics</key>
<string>Bruce Baum</string>
<key>eventGoodies</key>
<string>Prizes</string>
<key>eventDate</key>
<date>2010-07-24T00:00:00Z</date>
</dict>
<dict>
<key>eventName</key>
<string>Late Night Film Series</string>
<key>eventLocation</key>
<string>The Cats Den</string>
<key>eventType</key>
<string>Comedy</string>
<key>eventSpecifics</key>
<string>Avatar</string>
<key>eventGoodies</key>
<string>Food</string>
<key>eventDate</key>
<date>2010-07-24T02:00:00Z</date>
</dict>
</array>
這裏是我的ThisWeekViewController.h:
#import <UIKit/UIKit.h>
@class Event;
@interface ThisWeekViewController : UITableViewController
{
NSArray *eventNames;
Event *event;
}
@property (nonatomic, retain) NSArray *eventNames;
@property (nonatomic, retain) Event *event;
@end
這裏是我ThisWeekViewController.m:
#import "ThisWeekViewController.h"
#import "Event.h"
@implementation ThisWeekViewController
@synthesize eventNames;
@synthesize event;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Events" ofType:@"plist"];
NSArray *dict = [[NSArray alloc] initWithContentsOfFile:path];
NSArray *namesArray = [dict valueForKey:@"eventName"];
self.eventNames = namesArray;
[dict release];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
self.eventNames = nil;
}
#pragma mark Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.eventNames count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [NSString stringWithFormat:@"%@", event.eventName];
return key;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.eventNames count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
// Set the cell's text to the event name
cell.textLabel.text = event.eventName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (void)dealloc {
// [thisWeek release];
[event release];
[eventNames release];
[super dealloc];
}
@end
我還包括了我的Event.h文件
#import <Foundation/Foundation.h>
@interface Event : NSObject {
NSString *eventName;
NSString *eventType;
NSDate *eventDate;
NSString *eventLocation;
NSString *eventGoodies;
NSString *eventSpecifics;
}
- (id)initWithDictionary:(NSDictionary *)aDictionary;
@property (nonatomic, retain) NSString *eventName;
@property (nonatomic, retain) NSString *eventType;
@property (nonatomic, retain) NSString *eventLocation;
@property (nonatomic, retain) NSDate *eventDate;
@property (nonatomic, retain) NSString *eventGoodies;
@property (nonatomic, retain) NSString *eventSpecifics;
@end
和我Event.m文件
#import "Event.h"
@implementation Event
@synthesize eventName;
@synthesize eventType;
@synthesize eventDate;
@synthesize eventLocation;
@synthesize eventGoodies;
@synthesize eventSpecifics;
- (id)initWithDictionary:(NSDictionary *)aDictionary {
if ([self init]) {
self.eventName = [aDictionary valueForKey:@"eventName"];
self.eventType = [aDictionary valueForKey:@"eventType"];
self.eventDate = [aDictionary valueForKey:@"eventDate"];
self.eventLocation = [aDictionary valueForKey:@"eventLocation"];
self.eventGoodies = [aDictionary valueForKey:@"eventGoodies"];
self.eventSpecifics = [aDictionary valueForKey:@"eventSpecifics"];
}
return self;
}
- (void)dealloc {
[eventName release];
[eventType release];
[eventDate release];
[eventLocation release];
[eventGoodies release];
[eventSpecifics release];
[super dealloc];
}
@end
我希望能夠把事情如cell.detailTextLabel.text = event.eventSpecifics
,只是流失這樣的引用。
不,將首先調用viewDidLoad並僅調用一次。因此,將[self.tableView reloadData]放入viewWillAppear:更準確 – vodkhang 2010-07-16 01:11:04
不能保證'viewDidLoad'在'viewWillAppear:'之前被調用,事實上證明這種情況非常容易。創建一個記錄兩種方法的UIViewController子類,並將其推送到導航堆棧上。上次我測試時,UINavigationController在它訪問視圖並觸發'viewDidLoad'之前調用'viewWillAppear:'。 – 2010-07-18 22:49:41
我加了Kevin Ballard的所有建議,表格視圖仍然是空的。我還加入了: - (void)viewWillAppear:(BOOL)animated { \t //強制tableview加載 \t [self.tableView reloadData]; } 根據vodkhang和表視圖仍然是空的。您可能會看到代碼中的任何其他錯誤? – 2010-07-20 20:35:36