Xcode 9无法识别TableViewCell标识符及cell未使用错误的解决求助
Hey Andy, let's work through these two Xcode 9 issues you're facing—they're both common gotchas, so you're in good company!
This error pops up for two clear reasons:
- You created the
cellvariable but didn’t do anything with it (no configuration, no return statement) - The
tableView(_:cellForRowAt:)method requires you to return a UITableViewCell as its output, which your current code is missing entirely.
Here’s the corrected code that fixes both problems:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // Use guard let to safely unwrap the reusable cell (avoids force-unwrap crashes) guard let cell = companyTableView.dequeueReusableCell(withIdentifier: "cell") else { // Fallback: create a default cell if reuse fails (rare if your identifier is set correctly) return UITableViewCell(style: .default, reuseIdentifier: "cell") } // Add your cell configuration here (e.g., set text, images) // cell.textLabel?.text = yourDataArray[indexPath.row] // Don't forget to return the configured cell! return cell }
If you’re confident the cell will always be available (since you set the identifier), you could use a force-unwrap (let cell = companyTableView.dequeueReusableCell(withIdentifier: "cell")!), but the guard let approach is safer for production code.
If Xcode isn’t picking up your "cell" identifier even after setting it in the storyboard, try these steps in order:
- Double-check the identifier spelling and case:Xcode is case-sensitive! Make sure the Reuse Identifier field in the Identity Inspector (for your TableViewCell) is exactly
"cell"—no capital letters, extra spaces, or typos. Also confirm this cell is nested inside yourcompanyTableViewin the storyboard (not attached to a different table view). - Clean your project and rebuild:Xcode 9 can hold onto stale cache data. Press
Cmd+Shift+Kto clean the build folder, thenCmd+Bto recompile. This often fixes identifier recognition glitches. - Validate your IBOutlet connection:Ensure your
companyTableViewoutlet is properly connected to the table view in your storyboard. If the connection is broken or linked to the wrong view, the table view won’t look for your cell identifier correctly. - Register the cell programmatically (fallback):If the storyboard approach still isn’t working, manually register the cell in
viewDidLoad()to bypass storyboard recognition issues:
override func viewDidLoad() { super.viewDidLoad() // Register the cell class with your identifier companyTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") }
This tells the table view exactly which cell class to use for the "cell" identifier, regardless of storyboard settings.
内容的提问来源于stack exchange,提问作者Andy Wagga

