我正在使用主详细信息模板。
头文件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)
(请注意,我没有包括所有内容,只包括我认为重要的部分)
这一直让我疯狂,我已经多次检查过,重写了整个事情,结果仍然相同,我甚至无法显示细胞。我用谷歌搜索,我发现这样的东西意味着我正在向一个不知道该怎么做的方法发送消息,但我确定它是对的?请有人帮我调试一下!
答案 0 :(得分: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"]];
答案 1 :(得分:-1)
如果您使用ARC NOT :您需要保留每个词典。像这样redFlowers = [[[NSMutableArray alloc] init] retain];
为每个数组执行此操作。此外,在您的应用崩溃并发布输出的代码行之前NSLog(@"%@", redFlowers)
。将redFlowers替换为接下来调用的字典。
答案 2 :(得分:-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
对需要添加的所有三个或多个对象重复此操作。希望这会有所帮助。