Swift UITableView每隔N个单元格不同

编程入门 行业动态 更新时间:2024-10-03 14:19:56
本文介绍了Swift UITableView每隔N个单元格不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正试图在第n个(第3个)单元格中显示一个不同的单元格(广告)。 像这样:

I'm trying to show a different cell (Ad) every nth (3rd) cell. Like this:

"Request" "Request" "Request" "Ad" "Request" "Request" "Request" "Ad" "Request" "Request" "Request" "Ad" "Request" ...

我的代码:

这些是我的变量

var requests = [RequestListable]() var list = [Listable]() var users = [UserInfo?]() var adInterval = 3

这是我尝试加载广告的功能。问题是通常每个请求(requestsArray)都有一个关联的用户(usersArray),当我将广告插入 listArray时,用户不再与请求匹配。一点也不。 这就是为什么我要在插入广告的相同索引处插入一个fakeUser的原因,但这是行不通的。

That's my function where I'm trying to load the ads. The problem is that normally every request(requestsArray) has one associated user (usersArray) and when I'm inserting the ads into the "listArray" the users are not matching with the requests anymore. Not at all. That's why I'm inserting a fakeUser at the same index, where the ads are getting inserted, but that doesn't work.

我正在使用其他函数重新加载TableView,该函数会提取我的请求

I'm reloading the TableView in a different function, which fetches my requests

func loadAds() { Api.adApi.observeAds { (ad, user) in self.list = self.requests for i in stride(from: self.adInterval, to: self.requests.count, by: self.adInterval).reversed() { self.list.insert(ad, at: i) self.users.insert(user, at: i) // This is probably where i'ts going wrong print(self.users.count) // Returns 40 (Don't know why) print(self.list.count) // Returns 13 } } }

这些是我的TableView函数

Those are my TableView functions

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return list.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let listing = list[indexPath.row] if let request = listing as? RequestListable { let cell = tableView.dequeueReusableCell(withIdentifier: "RequestCell", for: indexPath) as! RequestTVCell let user = users[indexPath.row] cell.request = request cell.user = user cell.delegate = self return cell } else if let ad = listing as? AdListable { let cell = tableView.dequeueReusableCell(withIdentifier: "AdCell", for: indexPath) as! AdTVCell cell.ad = ad cell.user = users[indexPath.row] return cell } fatalError("Did not find data or ad for cell: Should never get here") }

所以问题是:

在插入广告和用户之后,用户与其关联的请求不匹配

The users are not matching with their associated requests after inserting the ads and the users

有什么建议吗?

推荐答案

您每次每次观察广告时都会用self.requests副本替换self.list。关闭,但总是插入self.users中。

You're replacing self.list with a copy of self.requests every time the observeAds closure is executed, while always inserting in self.users.

更多推荐

Swift UITableView每隔N个单元格不同

本文发布于:2023-11-26 09:56:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:每隔   单元格   Swift   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!