我有一个包含 5 个部分的 UICollectionView,一些部分有数据,一些部分(在我的代码中是第 2 部分)没有(它取决于服务器)
因此,我想在没有数据的选择中显示一个标签(“无项目”)。
但是,我可以找到任何想法来做到这一点,我希望任何人都可以给我一些建议或指导来实现它。
我真的很感激任何帮助
这是我的 intergrade 部分的代码
-(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
FriendsFanLevelHeaderView *headerView = (FriendsFanLevelHeaderView *)[self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FanLevelHeader" forIndexPath:indexPath];
switch (indexPath.section) {
case 0:
[headerView.lblFanLevelTitle setText:@"Gold"];
break;
case 1:
[headerView.lblFanLevelTitle setText:@"Silver"];
break;
case 2:
[headerView.lblFanLevelTitle setText:@"Bronze"];
break;
case 3:
[headerView.lblFanLevelTitle setText:@"Green"];
break;
case 4:
[headerView.lblFanLevelTitle setText:@"Other"];
break;
default:
break;
}
return headerView;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
switch (section) {
case 0:
return 3;
case 1:
return 0; // it doesn't have any item
case 2:
return 2;
case 3:
return 3;
case 4:
return 5;
default:
return 0;
}
}
- (FriendsCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
FriendsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsCollectionViewCell" forIndexPath:indexPath];
[cell.lblFriendBand setText:@"Band: White Mash "];
[cell.lblFriendGenre setText:@"Freestyle house, House, Freestyle music,"];
[cell.lblFriendECScore setText:@"EC score: 79"];
return cell;
}
============================================
这就是我想要的
最佳答案
假设您在 NSArray 中有每个部分的数据(项目)。
所以你有 goldSectionItems 数组、silverSectionItems 数组、bronzeSectionItems 数组、greenSectionItems 数组和 otherSectionItems 数组。
你要做的是:
在情况 1 中,您想要使用包含您的项目的数组向 Collection View 指示您的部分中的项目数。
在情况 2 中,您想向 Collection View 指示您有 1 个项目,这将是“无项目”单元格。
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
switch (section) {
case 0:
return MAX(1, goldSectionItems.count);
case 1:
// return at least 1 when you have no items from the server.
// When you do not have any items in
// you NSArray then you return 1, otherwise you return
// the number of items in your array
return MAX(1, silverSectionItems.count);
case 2:
return MAX(1, bronzeSectionItems.count);
case 3:
return MAX(1, greenSectionItems.count);
case 4:
return MAX(1, otherSectionItems.count);
default:
return 0;
}
}
注意 MAX 将返回其两个操作数之间的最大值。例如,如果您的 silverSectionItems 数组为空,则 count 属性将返回 0,因此 MAX(1, 0) 将返回 1。如果您的 silverSectionItems 不为空,count 将返回 N(其中 N>1)所以 MAX(1, N) 将返回 N。
然后在您的 -collectionView:cellForItemAtIndexPath: 中,您要检查您是哪种情况:
如果您属于情况 1,您需要一个显示正常内容的单元格。
如果您属于情况 2,您需要一个显示“无项目”的单元格。
- (FriendsCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
FriendsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FriendsCollectionViewCell" forIndexPath:indexPath];
// get the array that contains the items for your indexPath
NSArray *items = [self itemArrayForIndexPath:indexPath];
// case 2
// if the section does not have any items then set up the
// cell to display "No item"
if (items.count == 0) {
[cell.lblFriendBand setText:@"No item"];
[cell.lblFriendGenre setText:@""];
[cell.lblFriendECScore setText:@""];
}
// case 1
// setup the cell with your items
else {
// get you item here and set up the cell with your content
// Item *item = items[indexPath.item];
[cell.lblFriendBand setText:@"Band: White Mash "];
[cell.lblFriendGenre setText:@"Freestyle house, House, Freestyle music,"];
[cell.lblFriendECScore setText:@"EC score: 79"];
}
return cell;
}
// return array for the corresponding indexPath
- (NSArray *)itemArrayForIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
return goldSectionItems;
case 1:
return silverSectionItems;
case 2:
return bronzeSectionItems;
case 3:
return greenSectionItems;
case 4:
return otherSectionItems;
default:
return nil;
}
}
-(UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
FriendsFanLevelHeaderView *headerView = (FriendsFanLevelHeaderView *)[self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FanLevelHeader" forIndexPath:indexPath];
switch (indexPath.section) {
case 0:
[headerView.lblFanLevelTitle setText:@"Gold"];
break;
case 1:
[headerView.lblFanLevelTitle setText:@"Silver"];
break;
case 2:
[headerView.lblFanLevelTitle setText:@"Bronze"];
break;
case 3:
[headerView.lblFanLevelTitle setText:@"Green"];
break;
case 4:
[headerView.lblFanLevelTitle setText:@"Other"];
break;
default:
break;
}
return headerView;
}
有不懂的就问。
关于ios - UICollectionView : Show label "No item" in the section that don't have any item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34526276/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request