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

时间:2016-02-03 02:05:52

标签: swift uitableview xcode7 uisegmentedcontrol

我有一个问题,即只为分段控制器的一个案例添加额外的自定义单元格。我想添加一个"添加项目"动作单元格只能在分段控制器的情况3上查看。有谁知道如何实现这个或在哪里我可以找到一个好的资源(除了Apple文档),看看如何做到这一点。谢谢!

导入UIKit

类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:

// ***我想在这里添加一个单元格"添加项目" IBAction ---------------------

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

    default:
        break

    }

    return myCell

}

1 个答案:

答案 0 :(得分:1)

To add new cell in your 3rd segment case do the following changes in your code.

In your switch case statement,

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

    break
}

Now in your 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
   }

In your didSelectRowAtIndexPath if you didn't use button approach.

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
   }

 }
}