2011-02-14 56 views
1

我們, 我正在爲一些問題而奮鬥,因爲有些日子我想要得到您的幫助。 我有一個正常解析的服務器上的RSS文件。基本上是關於表演,音樂會,樂隊演出。這就是爲什麼我創建了顯示級:如何從NSDictionary中檢索對象來設置表格

Show.h:

#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 

@interface Show : NSObject { 

    NSString* gigName; 
    NSString* date; 
    NSString* place; 
    NSString* door; 
    NSString* time; 
    NSString* bands; 
    NSString* entry; 
    NSString* query; 
    NSString* flyer; 
    NSString* type; 
} 

- (id)init:(NSString*)gigName date:(NSString*)date place:(NSString*)place door:(NSString*)door time:(NSString*)time bands:(NSString*)bands 
    entry:(NSString*)entry query:(NSString*)query flyer:(NSString*)flyer type:(NSString*)type; 

@property (nonatomic, retain) NSString* gigName; 
@property (nonatomic, retain) NSString* date; 
@property (nonatomic, retain) NSString* place; 
@property (nonatomic, retain) NSString* door; 
@property (nonatomic, retain) NSString* time; 
@property (nonatomic, retain) NSString* bands; 
@property (nonatomic, retain) NSString* entry; 
@property (nonatomic, retain) NSString* query; 
@property (nonatomic, retain) NSString* flyer; 
@property (nonatomic, retain) NSString* type; 

@end 

和Show.m:

#import "Show.h" 

@implementation Show 
@synthesize gigName, date , place, door, time, bands, entry, query, flyer, type; 

- (id)init:(NSString*)_gigName date:(NSString*)_date place:(NSString*)_place door:(NSString*)_door time:(NSString*)_time bands:(NSString*)_bands 
    entry:(NSString*)_entry query:(NSString*)_query flyer:(NSString*)_flyer type:(NSString*)_type { 
    //if (self = [super init]) { 
    self = [super init]; 

    if(self) { 
     gigName = _gigName; 
     date = _date; 
     place = _place; 
     door = _door; 
     time = _time; 
     bands = _bands; 
     entry = _entry; 
     query = _query; 
     flyer = _flyer; 
     type = _type; 
    } 
    return self; 
} 

- (void)dealloc { 
    [super dealloc]; 
} 
@end 

在我TourViewController(這是一個表視圖)我有創建此:

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    //Initialize the array. 
    listOfItems = [[NSMutableArray alloc] init]; 

    pastShowArray = [NSMutableArray arrayWithCapacity:[stories count]]; // needs to be mutable 
    futureShowArray = [NSMutableArray arrayWithCapacity:[stories count]]; // needs to be mutable 


    for(int i = 0; i < [stories count]; i++) { 

     // get fields from story parser object 
     gigName = [[stories objectAtIndex:i] objectForKey:@"gig"]; 
     type = [[stories objectAtIndex:i] objectForKey:@"type"]; 
     date = [[stories objectAtIndex:i] objectForKey:@"date"]; 
     place = [[stories objectAtIndex:i] objectForKey:@"place"]; 
     door = [[stories objectAtIndex:i] objectForKey:@"door"]; 
     time = [[stories objectAtIndex:i] objectForKey:@"time"]; 
     bands = [[stories objectAtIndex:i] objectForKey:@"bands"]; 
     entry = [[stories objectAtIndex:i] objectForKey:@"entry"]; 
     query = [[stories objectAtIndex:i] objectForKey:@"query"]; 
     flyer = [[stories objectAtIndex:i] objectForKey:@"flyer"]; 

     // remove unnecessary whitespaces 
     gigName = [gigName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     type =  [type stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     date =  [date stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     place =  [place stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     door =  [door stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     time =  [time stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     bands =  [bands stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     entry =  [entry stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     query =  [query stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     flyer =  [flyer stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

     NSLog(gigName); 
     NSLog(type); 

     Show* show = [[Show alloc] init:gigName date:date place:place door:door time:time bands:bands entry:entry query:query flyer:flyer type:type]; 

     if([type isEqualToString:@"past"]) { 
      // fill past show array 
      NSLog(@"adding past object now"); 

      [pastShowArray addObject:show]; 

     } else { 
      // fill future show array 
      NSLog(@"adding future object now"); 

      [futureShowArray addObject:show]; 
     } 
    } 

    NSMutableDictionary *pastShowsArrayDict = [NSMutableDictionary dictionaryWithObject:pastShowArray forKey:@"Show"]; 
    NSLog(@"added past show array to pastshowarraydict"); 

    NSMutableDictionary *futureShowsArrayDict = [NSMutableDictionary dictionaryWithObject:futureShowArray forKey:@"Show"]; 
    NSLog(@"added future show array to futureshowarraydict"); 

    [listOfItems addObject:pastShowsArrayDict]; 
    [listOfItems addObject:futureShowsArrayDict]; 

    //Set the title 
    self.navigationItem.title = @"Tour"; 
} 

據我所知,這一切工作都沒有問題。現在我想在表格中顯示節目的「gigName」。爲此,我需要從NSDictionary獲取顯示對象。我有這個,但它崩潰:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Set up the cell... 
    NSLog(@"try to set up cell"); 
    //First get the dictionary object 
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section]; 
    NSArray *array = [dictionary objectForKey:@"Show"]; 

    Show *show = [array objectAtIndex:indexPath.row]; 
    NSLog(show.gigName); 
    cell.text = show.gigName; 

    return cell; 
} 

錯誤輸出:

2011-02-14 16:32:44.462 All in Vain [405:307] try to set up cell 
terminate called after throwing an instance of 'NSException' 
Program received signal: 「SIGABRT」. 

你們能告訴我怎樣才能得到展示對象,並設置表格文本show.gigName? 任何幫助appreaciated。謝謝你,

歡呼聲, doonot

回答

0

在你init:...顯示對象,你需要使它:

self.gigName = _gigName; 
self.date = _date; 
self.place = _place; 
self.door = _door; 
self.time = _time; 
self.bands = _bands; 
self.entry = _entry; 
self.query = _query; 
self.flyer = _flyer; 
self.type = _type; 

這樣他們每個人都保留(或做gigName = [_gigName retain];,但如果它不是空的,然後你釋放釋放)。

你也可能在視圖控制器和變量被設置的方式中有同樣的問題,但沒有看到更多的我不能確定。

+0

感謝隊友,解決了問題!我認爲我的整個對象吸氣劑是錯誤的。 – doonot 2011-02-14 20:58:15

0

試圖改變這一點:

cell.text = show.gigName; 

到:

cell.textLabel.text = show.gigName; 
+0

編程接收信號:「EXC_BAD_ACCESS」。 – doonot 2011-02-14 16:35:50