Swift UITableView Every Nth Cell Different

2019-08-29 12:09发布

问题:

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.

回答1:

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



回答2:

It looks like you have more users than requests and since you are using the indices of the arrays to match user and request, these aren't matching up. You have a comment 'Returns 40 (Dont know why) - you should figure this out and make sure your users are matching with the requests. Also, you may want to consider more tightly coupling your users and requests. Rather than storing them in separate arrays, you could use a tuple type for your list and add a method to make sure this is the correct user for the request, then you would always know you have the right user for the request.

var list = [(UserInfo, Listable)]()