我在 stackoverflow 上搜索了有关如何执行此操作的提示,并且 I found this question here但这并没有解决我的问题,我想要同样的东西(每 5 行添加一个广告 after),我尝试了那里提到的解决方案,但我得到的是第一行被广告取代,如第5等,我的正常内容正在被广告取代。 所以我想实现这个:
row 0 = normal content
row 1 = normal content
row 2 = normal content
row 3 = normal content
row 4 = normal content
row 5 = ad
row 6 = normal content
...
这是我尝试过的:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath.row % 5 == 0 && indexPath.row != 0) {
if let cellBanner = tableView.dequeueReusableCell(withIdentifier: "cellBanner", for: indexPath) as? BannerCell {
return cellBanner
} else {
return UITableViewCell()
}
} else {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? AnuncioCell {
if AnunciosService.instance.anuncios.count >= indexPath.row {
let anuncio = AnunciosService.instance.anuncios[indexPath.row]
let rowsToLoadFromBottom = 5;
let rowsLoaded = AnunciosService.instance.anuncios.count
if (!self.fetchingMore && (indexPath.row - (indexPath.row / 5) >= (rowsLoaded - rowsToLoadFromBottom))) {
let totalRows = AnunciosService.instance.anunciosCount
let remainingAnunciosToLoad = totalRows - rowsLoaded;
if (remainingAnunciosToLoad > 0) {
self.loadMoreAnuncios()
}
}
cell.configureCell(anuncio: anuncio)
let checkValue = Double(anuncio.user.nota)!
if checkValue < 1.0 {
cell.notaView.isHidden = true
}else{
cell.notaView.isHidden = false
}
if anuncio.destaque {
cell.destaque.isHidden = false
} else {
cell.destaque.isHidden = true
}
if anuncio.status == 0 {
if cell.imgAnuncio.image != nil{
Noir(originalImage: cell.imgAnuncio)
}
cell.sold_out.isHidden = false
cell.selectionStyle = .none
cell.isUserInteractionEnabled = false;
} else {
cell.sold_out.isHidden = true
cell.isUserInteractionEnabled = true;
cell.selectionStyle = .default
}
}
return cell
} else {
return UITableViewCell()
}
}
}
编号的行数:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if AnunciosService.instance.anuncios.count == 0 {
return 0
}
return AnunciosService.instance.anuncios.count + BannersService.instance.banners.count
}
最佳答案
我做错了,对于那些想知道我是如何解决它的人:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let index = indexPath.row + 1
if (index % 6 == 0 && indexPath.row != 0 && index/6 <= BannersService.instance.banners.count) {
let cellBanner = tableView.dequeueReusableCell(withIdentifier: "cellBanner", for: indexPath) as! BannerCell
return cellBanner
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AnuncioCell
let index = indexPath.row - (indexPath.row / 6)
if AnunciosService.instance.anuncios.count > index {
let anuncio = AnunciosService.instance.anuncios[index]
let rowsToLoadFromBottom = 5;
let rowsLoaded = AnunciosService.instance.anuncios.count
if (!self.fetchingMore && (index >= (rowsLoaded - rowsToLoadFromBottom))) {
let totalRows = AnunciosService.instance.anunciosCount
let remainingAnunciosToLoad = totalRows - rowsLoaded;
if (remainingAnunciosToLoad > 0) {
self.loadMoreAnuncios()
}
}
cell.configureCell(anuncio: anuncio)
let checkValue = Double(anuncio.user.nota)!
if checkValue < 1.0 {
cell.notaView.isHidden = true
}else{
cell.notaView.isHidden = false
}
if anuncio.destaque {
cell.destaque.isHidden = false
} else {
cell.destaque.isHidden = true
}
if anuncio.status == 0 {
if cell.imgAnuncio.image != nil {
Noir(originalImage: cell.imgAnuncio)
}
cell.sold_out.isHidden = false
cell.selectionStyle = .none
cell.isUserInteractionEnabled = false;
} else {
cell.sold_out.isHidden = true
cell.isUserInteractionEnabled = true;
cell.selectionStyle = .default
}
}
return cell
}
}
关于ios - 在 tableview 中每 5 个单元格后添加一个广告替换内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53567357/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R