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"
...
My Code:
those are my variables
var requests = [RequestListable]()
var list = [Listable]()
var users = [UserInfo?]()
var adInterval = 3
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.
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
}
}
}
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")
}
So the problem is:
The users are not matching with their associated requests after inserting the ads and the users
Any suggestions? I'd really appreciate it.