2014-10-01 52 views
0

我作爲示例文件從Github ENSwiftSideMenu下載,以獲取可用菜單的提示,MyMenuTableViewController.swift文件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

只需使用數組,並把從那裏

//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) 
} 
+0

謝謝您立即作出反應,我誆騙這個東西代替上面的代碼,但我還有一個問題,我添加了一個額外的列來包含圖像,因爲我插入選擇的圖像以匹配字符串? 在此先感謝您的幫助。 – 2014-10-01 11:33:49

相關問題