Why can't I access my struct data to populate cells?

Hi! I am new to Swift (and programming). I managed to successfully retrieve data from a new API – I want to then populate my table cells with a news title for each cell using the data in my struct FrontPage

I have attached screenshots of my News Model and my ViewController below. I am sorry if this is super basic stuff – I have watched YouTube videos and even tried reading the Swift documentation, but I really still don't understand anything and I have been stuck on this for three days now...

Check this out.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
	
	let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)

	let title = posts.hits[indexPath.row].title // Look here

	cell.textLabel?.text = title
	return cell
}

The code you posted always uses the first element in your array, the API of UITableView expects you to use the indexPath argument of the function.

Hello Daniel,

Thank you for being kind enough to reply - I know for experienced developers these basic questions might be annoying.

I tried your code but unfortunately the cells just end up being completely blank when I run the app :/

I know for a fact that the JSON data is being run via my getNews() function as I can print all the results in my console, but I can't get it to display within the cell

Nothing in the code you posted updates the posts variable, so you’re not getting any content. You need to update the value and tell the table to reload to see new content.

The API design of UITableView is very elegant, but not really easy to understand if you are a beginner. So your question is more than legitimate, unfortunately the Swift Forum is not the best place for it. You better ask your question here https://discussions.apple.com/community/developer_forums.

Since your question is primarily about UIKit and less about Swift as a language, I know that as a beginner it is hard to tell the difference.