Welcome to SO.
You are using a function, findObjectsInBackground
, that takes a completion closure. The completion closure is another function inside your cellForRowAt function. That function holds onto (captures) the completion closure you pass in and doesn’t call it until the background call is complete.
The block you pass in does not return a value. That’s what
(objects, error) -> Void in
means. It says “this block takes 2 parameters, but does not return anything.”
You can’t return cell from inside that completion closure.
Instead, what you want to do is to call findObjectsInBackground, then, outside of it’s block, finish configuring the cell and return it.
inside the completion closure for findObjectsInBackground, you should fetch the cell from the table view using tableView.cell(at:)
and install your newly fetched data into the cell.
EDIT:
I just noticed that you have code in your completion handler that ties to loop through your returned objects and create multiple cells. That won’t work at all. you’ll need to rethink your design.
The cellForRow(at:)
function must fetch, configure and return a single cell. The async method that you’re using won’t complete until after your cellForRow(at:)
function returns, so you’ll need to return a cell without the data you are loading, and then install the data into the cell inside your completion handler.
2
solved non-void return value in void function table view