如何传递数据并向TableView添加行且保留旧行?添加行被覆盖求助
Hey, let's figure out why your TableView only holds the latest entry—it’s almost certainly a data source management issue. Here’s the breakdown of what’s probably going wrong and how to fix it:
1. You’re Reinitializing the Data Array Instead of Appending to It
The biggest culprit here is likely that every time you save new data from the popup, you’re creating a brand new array instead of adding the new item to your existing one.
For example, if you’re doing this in your save callback:
// ❌ Wrong: Replaces the entire array with just the new item var items = [LinkItem]() items = [newItem]
You’ll only ever have one entry in the array. Instead, declare your data array as a class-level property in your TableViewController so it persists across saves, then append new items to it:
// ✅ Correct: Declare the array as a persistent property class YourTableViewController: UITableViewController { // Match this data model to your setup struct LinkItem { let name: String let link: String } // This array stores all your entries long-term private var savedItems = [LinkItem]() // ... rest of your TableVC code }
2. Append New Data Instead of Replacing the Array
When you receive the name and link from your popup, make sure you add the new item to your existing array—not replace the whole thing:
// Example callback for your popup's save button popupViewController.onSave = { [weak self] name, link in guard let self = self, !name.isEmpty, !link.isEmpty else { return } let newItem = LinkItem(name: name, link: link) // ✅ Append to the existing array (don't replace it) self.savedItems.append(newItem) // Refresh the TableView to show the new row self.tableView.reloadData() }
If you were using savedItems = [newItem] before, that’s exactly why old entries kept getting overwritten—this line replaces the entire array with just the new item.
3. Tie TableView Row Count to Your Array’s Length
Double-check your TableView data source methods to ensure you’re returning the actual length of your array, not a fixed number:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // ✅ Return the count of your data array, not a hardcoded 1 return savedItems.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellID", for: indexPath) let item = savedItems[indexPath.row] // Populate cell with your data cell.textLabel?.text = item.name cell.detailTextLabel?.text = item.link return cell }
If you had return 1 hardcoded here, your TableView would only ever show one row, no matter how many items are in your array.
Quick Recap
- Keep your data array as a persistent class property (don’t reinitialize it every time you save)
- Append new items to the array instead of replacing the entire array
- Make sure your TableView’s row count uses the array’s actual length
That should fix the issue and let you add as many rows as you want!
内容的提问来源于stack exchange,提问作者Ahmed

