我从Github ENSwiftSideMenu下载了一个示例文件来获取我应用程序中可能菜单的提示,文件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)
}
答案 0 :(得分:0)
只需使用数组并从中获取字符串文本
//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)
}