TableView動態&靜態cell, 以及錯誤訊息“Outlets cannot be connected to repeating content iOS”

Anny
Oct 16, 2020

--

本篇內容會提到的

  • Outlets cannot be connected to repeating content iOS 錯誤訊息
  • 原因與解決方法
  • TableView動態cell V.S. 靜態cell

Outlets cannot be connected to repeating content iOS

最近在開發App 的時候遇到上述的錯誤訊息

是在將元件 連接至TableViewController時出現的錯誤

來看看這是什麼問題吧!

情況

TableView cell 裡的元件 連結至程式碼 出錯

原因

  • 無法將 prototype cell 的 button 連接到 UIViewController 或者 中,必須將接到UITableviewCell。
  • 因為是使用Dynamic Prototypes來動態產生Cell,所以無法直接將Outlet連結至程式,因為會有多個Cell,不確定是哪一個Cell 的元件會連接到Outlet。

解決方法

Step1:先建立 UITableViewCell 的子類別

Step2:prototype cell 連結至剛建立的class

Step3:將元件 @IBOutlet 拉進class

這樣就可以使用對該元件執行操作囉~~~

在裡面設置 lable的文字 以及button的文字

TableView動態cell V.S. 靜態cell

TableView 動態cell V.S. 靜態cell

TableView動態cell

  • 如果今天想要顯示的Cell數量不固定時,例如To Do List 這種會增加減少的cell,就要使用Dynamic Prototypes,來動態產生Cell。
  • 像上述問題一樣無法直接將Outlet連結到程式,Cell是動態⽣成的,會有多個Cell,不確定是哪一個Cell 的元件會連接到Outlet。
  1. 動態cell 設定為下列形式

2. 可以在ViewController 加入 TableViewController 的 class 中

3. 在動態 cell 中加入 元件 @IBOutlet 以已經在上述問題中示範過囉~~~

TableView靜態cell

  1. 靜態cell 設定為下列形式

2. 只能在 TableViewController 的 class 中

3. 來示範 如何在靜態 cell 中加入 元件 @IBOutlet

Step1:創建 TableViewController class 並連結該class

Step2:可以直接將Outlet連結到程式

Step3:或者使用TableViewCell中的提供一個Tag值

let label = cell.viewWithTag(1) as! UILabellabel.text =”Hello World!”

--

--

Anny
Anny

Written by Anny

If You Think You Can, You Can!

No responses yet