2017-10-12 197 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中完成。 –