swift - Add custom cell to only one case of Segmented Controller -
i have question adding additional custom cell 1 case segmented controller. want add "add item" action cell tableview on case 3 segmented controller. know how implement or can find resource (besides apple documentation) see how this. thanks!
import 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() // additional setup after loading view, typically nib. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can 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: // *** want add cell here "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:
// *** want add cell here "add item" ibaction ---------------------
mycell.textlabel?.text = melist[indexpath.row] break default: break } return mycell }
to add new cell in 3rd segment case following changes in code.
in switch case statement,
func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { case 2: returnvalue = melist.count +1 break }
now in 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 cell's subview */ // set frame of button // set target , selector method of button [cell addsubview: addbutton] } else { mycell.textlabel?.text = melist[indexpath.row] } break }
in didselectrowatindexpath if didn't use button approach.
func tableview(tableview: uitableview, didselectrowatindexpath indexpath: nsindexpath) { switch(mysegmentedcontrol.selectedsegmentindex) { case 2: if(indexpath.row == melist.count) { // work here, click event of "add item" cell } } }
Comments
Post a Comment