如何从 NSDictionary 检索对象来设置表

发布于 2024-10-17 11:37:42 字数 6489 浏览 2 评论 0原文

我伙计们, 几天来我一直在与一个问题作斗争,希望得到你的帮助。 我的服务器上有一个 RSS 文件,可以正常解析。基本上都是关于演出、音乐会、乐队的演出。这就是为什么我创建了 Show-Class:

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 获取 show 对象。我有这个,但它崩溃了:

- (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 吗? 任何帮助表示赞赏。谢谢,

欢呼, 杜诺特

I guys,
I am fighting with a problem since some days and wanted to get your assistance.
I have an RSS file on a server which gets parsed normally. Basically it's about shows, concerts, gigs of a band. That's why I have created the Show-Class:

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

and the 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

In my TourViewController (which is a table view) i have created this:

- (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";
}

As far as I can tell, that worked all without problems. Now I want to show the "gigName" of the show in the table. For this I need to get the show object from the NSDictionary. I have this but it crashes:

- (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;
}

Error Output:

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”.

Can you guys tell me how I can get the show object and set the table text to show.gigName?
Any help is appreaciated. Thank you,

cheers,
doonot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水溶 2024-10-24 11:37:42

Show 对象的 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 keep] ; 但如果它不为空,您就会释放释放)。

您可能在视图控制器中也遇到相同的问题以及在那里设置变量的方式,但没有看到更多我无法确定。

In your init:... for the Show object you need to make it:

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;

so that each of them are retained (or do gigName = [_gigName retain]; but then you loose the release if it was not empty).

You may also have the same problem in the view controller and the way the variables are being set there but without seeing more I can't be sure.

夏了南城 2024-10-24 11:37:42

尝试将其更改

cell.text = show.gigName;

为:

cell.textLabel.text = show.gigName;

Try to change this :

cell.text = show.gigName;

into :

cell.textLabel.text = show.gigName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文