Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 更改菜单单元格标签的文本_Ios_Menu_Swift - Fatal编程技术网

Ios 更改菜单单元格标签的文本

Ios 更改菜单单元格标签的文本,ios,menu,swift,Ios,Menu,Swift,我从Github EnswitftSideMenu下载了一个示例文件,以提示我的应用程序中可能有一个菜单,即文件MyMenuTableViewController.swift。我想更改菜单单元格中的字符串,但在代码部分之后,我被卡住了。谢谢大家的帮助。萨尔瓦多 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

我从Github EnswitftSideMenu下载了一个示例文件,以提示我的应用程序中可能有一个菜单,即文件MyMenuTableViewController.swift。我想更改菜单单元格中的字符串,但在代码部分之后,我被卡住了。谢谢大家的帮助。萨尔瓦多

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
        cell!.backgroundColor = UIColor.clearColor()
        cell!.textLabel?.textColor = UIColor.darkGrayColor()
        let selectedBackgroundView = UIView(frame: CGRectMake(0, 0, cell!.frame.size.width, cell!.frame.size.height))
        selectedBackgroundView.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.2)
        cell!.selectedBackgroundView = selectedBackgroundView
    }

    cell!.textLabel?.text = "CELL \(indexPath.row+1)"

    return cell!
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 50.0
}

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

    println("did select row: \(indexPath.row)")

    if (indexPath.row == selectedMenuItem) {
        return
    }
    selectedMenuItem = indexPath.row

    //Present new view controller
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
    var destViewController : UIViewController
    switch (indexPath.row) {
    case 0:
        destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as UIViewController
        break
    case 1:
        destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Profilo") as UIViewController
        break
    case 2:
        destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Notizie") as UIViewController
        break
    default:
        destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Impostazioni") as UIViewController
        break
    }
    sideMenuController()?.setContentViewController(destViewController)
}

只需使用数组并从中获取字符串文本

//Declare array
var yourArray = ["string1","string2","string3","string4"]

//In cellForRowAtIndexPath  
cell!.textLabel?.text = yourArray[indexPath.row]
将上面的代码替换为下面的代码

var yourArray = ["string1","string2","string3","string4"]
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.
    return 4
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell

    if (cell == nil) {
        cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CELL")
        cell!.backgroundColor = UIColor.clearColor()
        cell!.textLabel?.textColor = UIColor.darkGrayColor()
        let selectedBackgroundView = UIView(frame: CGRectMake(0, 0, cell!.frame.size.width, cell!.frame.size.height))
        selectedBackgroundView.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.2)
        cell!.selectedBackgroundView = selectedBackgroundView
    }

    cell!.textLabel?.text = yourArray[indexPath.row]//"ViewController #\(indexPath.row+1)"

    return cell!
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 50.0
}

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

println("did select row: \(indexPath.row)")

if (indexPath.row == selectedMenuItem) {
    return
}
selectedMenuItem = indexPath.row

//Present new view controller
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main",bundle: nil)
var destViewController : UIViewController
switch (indexPath.row) {
case 0:
    destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as UIViewController
    break
case 1:
    destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Profilo") as UIViewController
    break
case 2:
    destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Notizie") as UIViewController
    break
default:
    destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("Impostazioni") as UIViewController
    break
}
sideMenuController()?.setContentViewController(destViewController)
}

谢谢你的即时回复我在这件事上被打入冷宫了,但我还有一个问题,我在表格中添加了一个额外的列来包含图像,因为我插入了与字符串匹配的所选图像?提前感谢你的帮助。