2010-07-15 406 views
2

我已经取得了一些进展,并且正在编辑问题以再次呈现。我现在最大的问题是分组表格视图将会加载,但是每行都显示每个部分的所有内容。此外,我的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,只是流失这样的引用。

回答

0

如果我没记错,UITableViewController重新加载-viewWillAppear:中的表格视图。在调用-viewDidLoad之前,可能会调用它。我建议您在-viewDidLoad的实施结束时插入对[self.tableView reloadData]的呼叫。

您还应该在-viewDidLoad的顶部执行[super viewDidLoad],同样也可以调用-viewDidUnload

此外,由于您的超类(UITableViewController)已经为您执行此操作,因此您无需声明您的班级符合UITableViewDataSourceUITableViewDelegate

+0

不,将首先调用viewDidLoad并仅调用一次。因此,将[self.tableView reloadData]放入viewWillAppear:更准确 – vodkhang 2010-07-16 01:11:04

+1

不能保证'viewDidLoad'在'viewWillAppear:'之前被调用,事实上证明这种情况非常容易。创建一个记录两种方法的UIViewController子类,并将其推送到导航堆栈上。上次我测试时,UINavigationController在它访问视图并触发'viewDidLoad'之前调用'viewWillAppear:'。 – 2010-07-18 22:49:41

+0

我加了Kevin Ballard的所有建议,表格视图仍然是空的。我还加入了: - (void)viewWillAppear:(BOOL)animated { \t //强制tableview加载 \t [self.tableView reloadData]; } 根据vodkhang和表视图仍然是空的。您可能会看到代码中的任何其他错误? – 2010-07-20 20:35:36

相关问题