我想在用户选择“其他”行时更改UIlabel的文本

时间:2016-06-27 10:56:02

标签: ios swift uitableview

enter image description here

当用户选择“其他”行时,我想更改UIlabel的文本。 然后我想将视图导航到下一个..我该怎么做..请帮助..

DIDSELECT FUNC:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    let cell:UITableViewCell =
        self.CatagoryTable.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
        Catagory1.CategoryNo = indexPath.row + 1
     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

    if(cell.textLabel == "Others"){
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("CamView") as! CamView

        self.navigationController?.pushViewController(nextViewController, animated:true)

    }
    else {
         let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("SubView") as! SubCatagory

        self.navigationController?.pushViewController(nextViewController, animated:true)
    }
        let backItem = UIBarButtonItem()
        backItem.title = "Back"
        backItem.tintColor? = UIColor.blueColor()
        navigationItem.backBarButtonItem = backItem

    }

2 个答案:

答案 0 :(得分:0)

您可以尝试这个,在didSelectRowAtIndexPath方法中编写此代码

let indexPath = tableView.indexPathForSelectedRow()
let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell

if(currentCell.textLabel!.text == "Others"){
// Do domething
}

答案 1 :(得分:0)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")
    let cell:UITableViewCell =
        self.CatagoryTable.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
        Catagory1.CategoryNo = indexPath.row + 1
     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

    cell.textLabel?.text = self.items[indexPath.row]
    let x = "Others"
    let y = cell.textLabel?.text

       if(x==y){

        let alert = UIAlertController(title: "Please Enter Item Name", message: "", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addTextFieldWithConfigurationHandler(configurationTextField)
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:handleCancel))
        alert.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
            print("Done !!")
            print("Item : \(self.tField.text)")
            let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("CamView") as! CamView

            self.navigationController?.pushViewController(nextViewController, animated:true)
        }))
        self.presentViewController(alert, animated: true, completion: {

            let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("CamView") as! CamView

            self.navigationController?.pushViewController(nextViewController, animated:true)
        })

    }
    else {
         let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("SubView") as! SubCatagory

        self.navigationController?.pushViewController(nextViewController, animated:true)
    }
        let backItem = UIBarButtonItem()
        backItem.title = "Back"
        backItem.tintColor? = UIColor.blueColor()
        navigationItem.backBarButtonItem = backItem

    }