Reputation: 174
Currently working on a Swift application, and I have built a slide out navigation menu using an API at https://github.com/mutualmobile/MMDrawerController. I'm trying to change the background image of each cell from the blank and boring white color into an image I choose of my own.
I have checked many resources from Stack Overflow self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]
which I inserted at the beginning of a case statement seems to produce the error of: Expected Expression From String Literal. Where would I insert a statement for background image and what would it be?
class LeftSideViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var menuItems:[String] = ["Daily Quote","Passion", "Failure","Dream","Grind","Hang on","Haters","Wisdom","Karma"];
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return menuItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var mycell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCustomTableViewCell
mycell.menuItemLabel.text = menuItems[indexPath.row]
return mycell;
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
switch(indexPath.row){
case 0:
var centerViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ViewController") as! ViewController
var centerNavController = UINavigationController(rootViewController: centerViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = centerNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 1:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]
var aboutViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PassionViewController") as! PassionViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 2:
var aboutViewController = self.storyboard?.instantiateViewControllerWithIdentifier("FailureViewController") as! FailureViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 3:
var aboutViewController =
self.storyboard?.instantiateViewControllerWithIdentifier("DreamViewController")
as! DreamViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 4:
var aboutViewController =
self.storyboard?.instantiateViewControllerWithIdentifier("GrindViewController")
as! GrindViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 5:
var aboutViewController =
self.storyboard?.instantiateViewControllerWithIdentifier("HangViewController")
as! HangViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 6:
var aboutViewController =
self.storyboard?.instantiateViewControllerWithIdentifier("HaterViewController")
as! HaterViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 7:
var aboutViewController =
self.storyboard?.instantiateViewControllerWithIdentifier("WisdomViewController")
as! WisdomViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
break;
case 8:
var aboutViewController =
self.storyboard?.instantiateViewControllerWithIdentifier("KarmaViewController")
as! KarmaViewController
var aboutNavController = UINavigationController(rootViewController: aboutViewController)
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.centerContainer!.centerViewController = aboutNavController
appDelegate.centerContainer!.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
default:
println("\(menuItems[indexPath.row]) is selected");
}
}
Upvotes: 1
Views: 140
Reputation: 3708
You can do it programmatically also by creating a custom class for tableview cell. for example inside your cellForRowAtIndexPath call the custom class
var imageArray = ["pic1.png", "pic2.png", "pic3.png"]
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var mycell = MyCustomClass(frame: CGRectMake(0, 0, self.view.frame.width, 50), row: indexPath.row, imageArray: imageArray)
mycell.menuItemLabel.text = menuItems[indexPath.row]
return mycell;
}
class MyCustomClass: UITableViewCell {
var menuItemLabel: UILabel!
init(frame: CGRect, row: Int, imageArray: Array<String>) {
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
//declare a image array that has the name of the image to be included
var image = imageArray[row]
backgroundColor = UIColor(patternImage: UIImage(named: "\(image)")!)
menuItemLabel = UILabel(frame: CGRectMake(0, 0, frame.width, 50))
addSubview(menuItemLabel)
}
}
Hope this help
Upvotes: 1
Reputation: 21520
First you have to set view background, then you have to set for each cell a transparent background color color:
override func viewDidLoad() {
super.viewDidLoad()
self.view.background = UIColor(patternImage: UIImage(named: "backgroundImage")!)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var mycell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCustomTableViewCell
mycell.backgroundColor = UIColor.clearColor()
mycell.menuItemLabel.text = menuItems[indexPath.row]
return mycell;
}
With a custom cell created on a xib (eg. "MyCustomTableViewCell.xib"), you have to do:
override func viewDidLoad() {
super.viewDidLoad()
let nib: UINib = UINib(nibName: "MyCustomTableViewCell", bundle: NSBundle.mainBundle())
tableView.registerNib(nib, forCellReuseIdentifier: "MyCustomTableViewCellIdentifier")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var mycell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCellIdentifier", forIndexPath: indexPath) as! MyCustomTableViewCell
...
}
And you have to set MyCustomTableViewCellIdentifier name on xib:
Upvotes: 0
Reputation: 958
Please try this following code :
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "imageName")!)
Upvotes: 0