仅将自定义单元格添加到分段控制器的一个案例

Add custom cell to only one case of Segmented Controller

我有一个关于向分段控制器的一个案例添加额外的自定义单元的问题。我只想在分段控制器的情况 3 上将 "Add Item" 操作单元添加到表视图。有谁知道如何实现这个或者我在哪里可以找到一个很好的资源(除了 Apple 文档)来了解如何做到这一点。谢谢!

导入 UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var mySegmentedControl: UISegmentedControl!
@IBOutlet weak var myTableView: UITableView!

let globalList: [String] = ["Global Item 1","Global Item 2", "Global Item 3"]
let friendsList: [String] = ["Friend 1", "Friend 2", "Friend 3"]
let meList: [String] = ["Milan, Italy", "Rome, Italy", "Napoli, Italy", "Paris, France"]

let addButton: [String] = ["+ Add item"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}





 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{


    var returnValue = 0

    switch(mySegmentedControl.selectedSegmentIndex)
    {

    case 0:
        returnValue = globalList.count
        break

    case 1:
        returnValue = friendsList.count
        break

    case 2:

        // *** I want to add a Cell here with an "Add Item" IBAction ---------------------

        returnValue = meList.count
        break

    default:
        break

    }

    return returnValue

}



@available(iOS 2.0, *)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)

    switch(mySegmentedControl.selectedSegmentIndex)

    {
    case 0:
        myCell.textLabel!.text = globalList[indexPath.row]
        break

    case 1:
        myCell.textLabel!.text = friendsList[indexPath.row]
        break

    case 2:

// *** 我想在这里添加一个带有 "Add Item" IBAction 的 Cell ------------------

        myCell.textLabel?.text = meList[indexPath.row]
        break

    default:
        break

    }

    return myCell

}

要在第 3 段案例中添加新单元格,请在代码中进行以下更改。

在你的 switch case 语句中,

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
  case 2:
    returnValue = meList.count +1

    break
}

现在在您的 cellForRowAtIndexPath 中

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
   let myCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)

   switch(mySegmentedControl.selectedSegmentIndex)
   {
       case 2:
       if(indexPath.row == meList.count) {
         myCell.textLabel?.text = "Add Item"  
         // add new row here 
         /* OR add new button here as cell's subview  */
           // set frame of your button
           // set target and selector method of button
           [cell addSubView: addButton]
       }
       else {
          myCell.textLabel?.text = meList[indexPath.row]
       } 

       break
   }

如果您没有使用按钮方法,请在您的 didSelectRowAtIndexPath 中。

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

 switch(mySegmentedControl.selectedSegmentIndex)
 {
   case 2:
   if(indexPath.row == meList.count) {
     // do your work here, this is the click event of your "Add Item" cell
   }

 }
}