Swift: How to use static cell in dynamic UITableView

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

static dynamic dynamic dynamic .. .. dynamic 

I've a dynamic table list with dynamic content. At the top of list, I need a static cell.

And trying to add a Label in my static cell to try features.

My static cell's identifier is: firstCell

Dynamic cell's identifier is: cell

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {     // #warning Potentially incomplete method implementation.     // Return the number of sections.     return 1 }  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     // #warning Incomplete method implementation.     // Return the number of rows in the section.     return 5 }   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell      // Configure the cell...      cell.textLabel?.text = "test"       return cell } 

This code return 5 times "test" in my dynamic cells. When I am try to add a Label and create outlet with static cell, get error.

I've googled and searched on stackoverflow but there is no swift version solution. How can do that?

EDIT FOR IMPROVE TO QUESTION:

Actually I need two section and section1 will be static content (I will create it programatically. E.g will use uidatepicker, uipicker and more..), section2 will work with just an dynamic array (this part is okey)

回答1:

Have you tried something like this?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell      if (indexPath.row == 1) {         cell.textLabel?.text = "test"     } else {         // Configure the cell...     }      return cell } 


回答2:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {         if indexPath.row == 0 {             var cell: IntroductionCell = self.tableView.dequeueReusableCellWithIdentifier("IntroCell") as! IntroductionCell             cell.labelCellText.text = textArray[indexPath.row].uppercaseString             return cell         } else {             var cell1: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("IntroCell1") as! UITableViewCell             return cell1         }     } 

Change the cell identifier for First Cell.

That's It LOL



回答3:

Override the functionality. Call super and change it up.

class TableViewController: UITableViewController {      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         var rowCount = super.tableView(tableView, numberOfRowsInSection: section)         if section == 1 {             rowCount -= 1         }         return rowCount     }      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {         let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)         return cell     }  } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!