在此先感谢您查看此项目并向我提供帮助。
我正在尝试在我的 Collection View 中显示一组 UIImage。我已经连接了 Storyboard 中的所有内容。
我的 ViewController 是我的 Collection View 的委托(delegate)和数据源。
我的单元格包含一个 UIImageView,它在我的自定义单元格的头文件中有一个 IBOutlet。
错误信息如下所示:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
*** First throw call stack:
(0x1c97012 0x10d4e7e 0x1c96deb 0x518748 0x533b1b 0x533646 0xf9ff8 0xfa232 0x5338d5 0x29a4 0x3ef6 0xb13fb4 0xb13e67 0x30ca 0x49fb53f 0x4a0d014 0x49fd7d5 0x1c3daf5 0x1c3cf44 0x1c3ce1b 0x1bf17e3 0x1bf1668 0x18ffc 0x1b9d 0x1ac5)
libc++abi.dylib: terminate called throwing an exception
我想知道我在创建 collectionView 时做错了什么导致它在稍后调用 reloadData 时崩溃。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UICollectionViewController <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIApplicationDelegate>
//receives a mutable array of UIImage(s)
-(void)receiveArrayOfImages: (NSMutableArray *)arrayOfImages;
@end
View Controller .m
#import "ViewController.h"
#import "Cell.h"
#import "GetImageURL.h"
NSString *kCellID = @"cellID";
@interface ViewController () {
UISearchBar *searchBar;
}
@property (nonatomic, strong) NSString *searchBarText;
@property (strong, nonatomic) NSMutableArray *arrayOfImages;
@end
@implementation ViewController
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if ([self.arrayOfImages count] > 0) {
NSLog(@"return %d items for collection view", [self.arrayOfImages count]);
return [self.arrayOfImages count];
} else {
NSLog(@"returned 0 items for collection view");
return 0;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForItemAtIndexPath");
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
int row = [indexPath row];
cell.cellImage.image = [self.arrayOfImages objectAtIndex:row];
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//Creates a Search Bar Button System Item.
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(tapSearchButton:)];
self.navigationItem.rightBarButtonItem = searchButton;
}
-(void)tapSearchButton:(id)sender {
//Adds or takes away searchbar on navigationbar.
if (!searchBar) {
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44.0)];
searchBarView.autoresizingMask = 0;
searchBar.delegate = self; //this line causes a warning. "Assigning to 'id<UISearchBarDelegate> from incompatible type 'ViewController *cons_strong"
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
[searchBar becomeFirstResponder];
} else {
self.navigationItem.titleView = nil;
searchBar = nil;
}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
GetImageURL *bookName = [[GetImageURL alloc] init];
bookName.bookName = [searchBar text]; //this line causes a warning. "Local declaration of 'searchBar' hides instance variable"
NSLog(@"In ViewController, the book name is: %@", bookName.bookName);
[bookName recieveBookUrl:bookName.bookName];
[self.view endEditing:YES];
}
-(void)receiveArrayOfImages: (NSMutableArray *)arrayOfImages {
//receives an array of UImages called arrayOfImages and sets it to self.arrayOfImages
self.arrayOfImages = arrayOfImages;
NSLog(@"This is the array of UIimages in ViewController.m: %@", self.arrayOfImages);
[self.collectionView reloadData]; //This line causes the app the crash. "Reason: UICollectionView must be initialized with a non-nil layout parameter"
}
@end
细胞.h
#import <UIKit/UIKit.h>
@interface Cell : UICollectionViewCell
//UIImageView inside Custom Cell
@property (strong, nonatomic) IBOutlet UIImageView *cellImage;
@end
细胞.m
#import "Cell.h"
@implementation Cell
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
}
return self;
}
@end
GetImageURL.h
#import <Foundation/Foundation.h>
#import "ViewController.h"
@interface GetImageURL : NSObject
@property (strong, nonatomic) NSString *bookName;
@property (strong, nonatomic) NSMutableArray *imageArray;
-(void)recieveBookUrl:(NSString *)bookName; //recieves a string from the viewController that the user inputs
@end
获取图片URL.m
#import "GetImageURL.h"
@interface GetImageURL()
@property (strong, nonatomic) NSString *pageID;
@property (strong, nonatomic) NSMutableArray *imageUrlArray;
@end
@implementation GetImageURL
//takes the string that the user entered and alters it to fit the wikipedia API format
-(void)recieveBookUrl:(NSString *)bookName {
self.imageUrlArray = [[NSMutableArray alloc] init];
self.imageArray = [[NSMutableArray alloc] init];
bookName = [bookName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //or use string by replacing @" " with @"%20";
NSString *wikiAPI = @"http://en.wikipedia.org/w/api.php?format=json&indexpageids&action=query&prop=images&titles=";
bookName = [wikiAPI stringByAppendingString:bookName];
NSURL *bookURL = [NSURL URLWithString:bookName];
//Plan to put this action in the background later
dispatch_async(dispatch_get_main_queue(), ^() {
NSData *data = [NSData dataWithContentsOfURL:bookURL];
[self performSelectorOnMainThread:@selector(getBookTitleApi:) withObject:data waitUntilDone:YES];
});
}
//first part of the API sucessfully gets the padeID and all the images returned by the API.
-(void)getBookTitleApi: (NSData *)data {
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSDictionary *parseQueryBookTitle = [json objectForKey:@"query"];
NSArray *parsePageIdsBookTitle = [parseQueryBookTitle objectForKey:@"pageids"];
for (NSDictionary *pageIdBookTitle in parsePageIdsBookTitle) {
self.pageID = [NSString stringWithFormat:@"%@", pageIdBookTitle];
}
NSDictionary *parsePagesBookTitle = [parseQueryBookTitle objectForKey:@"pages"];
NSDictionary *parsePageIdBookTitle = [parsePagesBookTitle objectForKey:self.pageID];
NSDictionary *parseImagesBookTitle = [parsePageIdBookTitle objectForKey:@"images"];
NSDictionary *valueTitleBookTitle = [parseImagesBookTitle valueForKey:@"title"];
for (NSDictionary *imageFilenames in valueTitleBookTitle) {
NSString *imageFilename = [NSString stringWithFormat:@"%@", imageFilenames];
imageFilename = [imageFilename substringFromIndex:5];
imageFilename = [imageFilename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //or use string by replacing @" " with @"%20";
NSString *wikiImageAPI = @"http://en.wikipedia.org/w/api.php?action=query&format=json&indexpageids&prop=imageinfo&iiprop=url&titles=Image:";
imageFilename = [wikiImageAPI stringByAppendingString:imageFilename];
NSURL *imageFilenameUrl = [NSURL URLWithString:imageFilename];
NSData *data = [NSData dataWithContentsOfURL:imageFilenameUrl];
[self performSelectorOnMainThread:@selector(getImageApi:) withObject:data waitUntilDone:YES];
}
//Uses the URLs to create an NSData object and then uses that data to create a UIImage which is added to a Mutable Array
for (NSString *jpgURL in self.imageUrlArray) {
NSURL *imageURL = [NSURL URLWithString:jpgURL];
NSLog(@"This is an imageURL in GetImageURL.m: %@", imageURL);
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
[self.imageArray addObject:image];
}
NSLog(@"The array of images in GetImageURL.m contains %d images called %@", [self.imageArray count], self.imageArray);
ViewController *view = [[ViewController alloc] init];
[view receiveArrayOfImages:self.imageArray]; //sends the array of images to the View Controller
//objects removed from array so that next user search can start over
[self.imageArray removeAllObjects];
[self.imageUrlArray removeAllObjects];
}
//Uses the image filenames from the first section and gets the specific URLS for the image. Limits results to .jpg images
-(void)getImageApi: (NSData *)data {
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *parseQueryImage = [json objectForKey:@"query"];
NSDictionary *parsePageIdsImage = [parseQueryImage objectForKey:@"pageids"];
for (NSString *pageIdImage in parsePageIdsImage) {
NSDictionary *parsePagesImage = [parseQueryImage objectForKey:@"pages"];
NSDictionary *parsePageIdImage = [parsePagesImage objectForKey:pageIdImage];
NSDictionary *parseImageInfoImage = [parsePageIdImage objectForKey:@"imageinfo"];
NSDictionary *valueImageInfoUrlsImage = [parseImageInfoImage valueForKey:@"url"];
for (NSString *valueImageInfoUrlImage in valueImageInfoUrlsImage) {
if ([valueImageInfoUrlImage hasSuffix:@".jpg"]) {
[self.imageUrlArray addObject:valueImageInfoUrlImage];
}
}
}
}
@end
很抱歉把所有的代码都放在这里。我认为如果一切都在那里会更容易。也许它对其他人也有帮助。
谢谢!
最佳答案
您是否将布局设置为流动?它在您的 Storyboard 中应该如下所示:
如果将其设置为自定义,则需要自己创建布局。
关于ios - 如何将 UIImages 数组添加到 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18001522/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack