ios swift:如何在dynamictableview中有一个静态单元格?

我想使用tableView在dynamic单元格列表的顶部有2个静态单元格。 据我所知,我不得不使用一个dynamic的原型tableView。 但我不明白如何添加2静态单元格,并devise它们,例如。 向第一个添加一个文本字段,向第二个添加一个标签。

我的故事板中有什么要做的? 在控制器内部我该做些什么? 我如何区分dynamic单元格的静态?

有人能给我一个血腥的初学者一些指导从哪里开始? 非常感谢!!

编辑:我试过这个testing:

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as CardTableViewCell //static cell if (indexPath.row < 2) { cell.dyn.text = "static \(indexPath.row)" return cell; } // Configure the cell... cell.dyn.text = "buh" return cell } 

结果如下: 在这里输入图像说明

后来当我使用真实的数据,我会错过前两个数据行…我可以以某种方式“重置”行计数器创build我的静态单元格后?

我该如何修改2个静态单元? 为了添加一个文本框和标签? 或者我必须以编程方式做到这一点?

我在这里find了帮助: 在分组表格视图中混合静态和dynamic部分

我的解决scheme如下所示:

1。 在这里输入图像说明

  1. 添加和布局静态单元格: 在这里输入图像说明

  2. 给每个单元格一个唯一的名称,并将它们作为出口添加到TableViewCell

  3. 调整代码:

     override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 3 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if (section == 2){ // my dynamic cell is index 2 return 5 // just for testing, add here yourrealdata.count } return 1 // for static content return 1 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: CardTableViewCell! if (indexPath.section == 0) { cell = tableView.dequeueReusableCellWithIdentifier("static1", forIndexPath: indexPath) as CardTableViewCell cell.cardSetName?.text = self.cardSetObject["name"] as String }else if (indexPath.section == 1) { cell = tableView.dequeueReusableCellWithIdentifier("static2", forIndexPath: indexPath) as CardTableViewCell // just return the cell without any changes to show whats designed in storyboard }else if (indexPath.section == 2) { cell = tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as CardTableViewCell cell.dyn.text = "row \(indexPath.row)" // return test rows as set in numberOfRowsInSection } return cell; } 

最终结果如下所示:

在这里输入图像说明

我希望我能帮助有同样问题的人:)

你可以使用这样的东西来使用或显示你的静态单元格

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numberOfDynamicCells + 1; } 

并在你cellForRowAtIndexPath数据源,你可以使用这样的东西。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.row == 0){ // go ahead to display your static Cell } else{ //go ahead to display your dynamic cells. } return yourCell; } 

这里是swift的代码。

 func numberOfRowsInSection(_ section: Int) -> Int{ return numberOfDynamicCells + 1 } 

并在你cellForRowAtIndexPath数据源,你可以使用这样的东西。

 func cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?{ if indexPath.row = 0{ // go ahead to display your static Cell } else{ //go ahead to display your dynamic cells. } return yourCell; } 

祝你好运…

是的,你可以通过让静态单元格为IBOutlet属性,并在tableView(_:cellForRowAt:)你可以返回你想要的任何索引path的属性

这是一个例子:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.row { case 0: return firstStaticCell case 1: return secondStaticCell default: let cell = tableView.dequeueReusableCell(withIdentifier: "DynamicCell", for: indexPath) cell.textLabel?.text = "Dynamic \(indexPath.row + 1)" return cell } } 

在这里输入图像说明