我正试图在第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个单元格不同
发布评论