2017-10-12 198 views
-1

我需要一個標題,描述和右側開關的單元格。我可以自行編程,但我想利用已經有說明的subtitle單元(detailTextLabel)。將UISwitch添加到樣式字幕的TableViewCell

但是,每當我嘗試在Interface Builder中添加UISwitch時,它都不起作用。首先,它不會讓我將交換機拖入單元。當我外拖動它,然後在Content View從文檔大綱(左側欄)插入,這似乎是內部的,但我不能添加約束: Cell with switch inside

我有一個SwitchCell.swift,看起來像這樣:

public class SwitchCell: UITableViewCell { 

    @IBOutlet var title: UILabel! 
    @IBOutlet var switch: UISwitch! 
... 
} 

我可以創建一個標題標籤的故事板鏈接,而不是一個開關。

有沒有人知道我必須修改爲了將附件標籤添加到字幕單元?它的工作原理與用cell.accessoryView.= UISwitch()控制器代碼,但我想知道爲什麼它是不可能在Interface Builder中

回答

-1

在Interface Builder中,你不能修改內置的細胞類型如」 subtitle已經有描述的單元格(detailTextLabel)「。如果要在Interface Builder中添加開關或進行其他內容修改,則必須使用「自定義」單元格。

可以,但是,繼續使用界面生成器subtitle細胞,但添加和代碼配置開關(在cellForRowAt:,例如)。

+0

這就是我已經做過的(正如我的問題所解釋的)。你碰巧知道爲什麼這是這樣嗎?這不是說單元格是不可修改的(可以從程序中修改)。 –

+1

這就是Interface Builder如何實現內置單元類型。我不明白爲什麼;我不爲Apple工作。我可以說,但那很愚蠢。如果您不喜歡它,請向Apple提交增強請求。 – matt

0

您可以通過編程來完成。

func switchChanged(_ sender : UISwitch!) 
{ 
     print("table row switch Changed \(sender.tag)") 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

       var cell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifire", for: indexPath) as! UITableViewCell 

         cell.textLabel?.text = "XYZ" 
         cell.detailTextLabel?.text = "XYZ" 

         let switchView = UISwitch(frame: .zero) 
         switchView.setOn(false, animated: true) 

         switchView.tag = indexPath.row 

         switchView.addTarget(self, action: #selector(self.switchChanged(_:)), for: .valueChanged) 
// YOU CAN ALOSO ADD CONSTRAINTS HERE 

         cell.accessoryView = switchView 

       return cell 
      } 
     return UITableViewCell() 
     } 
+0

請閱讀我的問題:我已經通過編程完成了它,但是無法從BI中完成。 –