0

我正在使用主详细信息模板。

头文件 MasterViewController.h:

#import <UIKit/UIKit.h>
//(Imported both MasterViewController.h and DetailViewController.h in implementation file of MasterViewController.m)
@class DetailViewController;

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) DetailViewController *detailViewController;
-(void)createFlowerData;
@end

实现文件HeaderViewController.m:

@interface MasterViewController () {
NSMutableArray *_objects;

NSArray *_flowerData;
NSArray *_flowerSections;
}
@end

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

//invoking the method I implemented to give data to flowerData array
[self createFlowerData];
}

-(void)createFlowerData{
NSMutableArray *redFlowers;
NSMutableArray *blueFlowers;

redFlowers = [[NSMutableArray alloc] init];
blueFlowers = [[NSMutableArray alloc] init];
//create the 2 sections for the flowerSections array
_flowerSections = @[@"Red Flowers", @"Blue Flowers"];

//add the objects to the mutable array
//red flowers
[redFlowers addObject:@{@"name":@"Poppy",@"picture":@"Poppy.png",@"url":@"http://en.wikiepdia.org/wiki/Poppy"}];
[redFlowers addObject:@{@"name":@"Tulip",@"picture":@"Tulip.png",@"url":@"http://en.wikipedia.org/wiki/Tulip"}];
[redFlowers addObject:@{@"name":@"Gerbera",@"picture":@"Gerbera.png",@"url":@"http://en.wikiepdia.org/wiki/Gerbera"}];
//blue flowers
[blueFlowers addObject:@{@"name":@"Phlox",@"picture":@"Phlox.png",@"url":@"http:en.wikipedia.org/wiki/Gerbera"}];
[blueFlowers addObject:@{@"name":@"Pin Cushion Flower",@"picture":@"Pincushion flower.png",@"url":@"http://en.wikipedia.org/wiki/Scabious"}];
[blueFlowers addObject:@{@"name":@"Iris",@"picture":@"Iris.png",@"url":@"http://en.wikipedia.org/wiki/Iris_(plant)"}];
_flowerData = @[redFlowers, blueFlowers];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
return [_flowerSections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //find the number of row  elements in a given section of the flower Data array
return [_flowerData[section] count];
}

 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//index 0 is the red flower
//index 1 is the blue flower
return _flowerSections[section];
}
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"flowerCell"];
cell.textLabel.text = _flowerData[indexPath.section][indexPath.row][@"name"];
cell.detailTextLabel.text = _flowerData[indexPath.section][indexPath.row][@"url"];
cell.imageView.image = _flowerData[indexPath.section][indexPath.row][@"picture"];

return cell;
}

然后,当我在 ios 模拟器(iPad)上构建应用程序时,我得到以下信息:

2013-09-01 23:49:40.015 flowerDetail2[2394:c07] -[__NSCFConstantString _isResizable]:      unrecognized selector sent to instance 0x6af4
2013-09-01 23:49:40.017 flowerDetail2[2394:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isResizable]: unrecognized selector sent to instance 0x6af4'
*** First throw call stack:
(0x1c95012 0x10d2e7e 0x1d204bd 0x1c84bbc 0x1c8494e 0x4ca7ab 0x121ae9 0x2c1f 0xd18fb 0xd19cf 0xba1bb 0xcab4b 0x672dd 0x10e66b0 0x2291fc0 0x228633c 0x2291eaf 0x1062bd 0x4eb56 0x4d66f 0x4d589 0x4c7e4 0x4c61e 0x4d3d9 0x502d2 0xfa99c 0x47574 0x4776f 0x47905 0x8dceab6 0x50917 0x1496c 0x1594b 0x26cb5 0x27beb 0x19698 0x1bf0df9 0x1bf0ad0 0x1c0abf5 0x1c0a962 0x1c3bbb6 0x1c3af44 0x1c3ae1b 0x1517a 0x16ffc 0x1bed 0x1b15)
libc++abi.dylib: terminate called throwing an exception
(lldb) 

(请注意,我没有包括所有内容,仅包括我认为重要的部分)

这让我一整天都发疯了,我检查了很多次,重写了整个东西,结果还是一样,我什至无法让单元格显示。我用谷歌搜索,我发现类似的东西意味着我正在向一个不知道如何处理它的方法发送消息,但我确定它是正确的?有人可以帮我调试一下吗!

4

3 回答 3

2
cell.imageView.image = _flowerData[indexPath.section][indexPath.row][@"picture"];

此行cell.imageView.image期望UIImage类型

[redFlowers addObject:@{@"name":@"Gerbera",@"picture":@"Gerbera.png",@"url":@"http://en.wikiepdia.org/wiki/Gerbera"}];

但是你在NSString这里给它一个,这会导致运行时错误。

所以应该是这样的

cell.imageView.image = [UIImage imageNamed:_flowerData[indexPath.section][indexPath.row][@"picture"]];
于 2013-09-02T04:48:49.630 回答
-1

如果您使用 ARC:您需要保留每个字典。像这样redFlowers = [[[NSMutableArray alloc] init] retain];为您的每个数组执行此操作。此外,NSLog(@"%@", redFlowers)在您的应用程序崩溃并发布输出的代码行之前。将 redFlowers 替换为接下来调用的字典。

于 2013-09-02T04:29:16.510 回答
-1

从我得到的,你需要做的是首先制作 NSDictionary 对象,然后将这些对象添加到 NSMutableArray。

     NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
     [dic setValue:@"Poppy" forKey:@"name"];
     [dic setValue:@"Poppy.png" forKey:@"picture"];
     [dic setValue:@"http://en.wikiepdia.org/wiki/Poppy" forKey:@"url"];

     [redFlowers addObject:dic];
     [dic release];     // Dont use this in case of ARC

对您需要添加的所有三个或多个对象重复此操作。希望这可以帮助。

于 2013-09-02T04:43:45.107 回答