Reputation: 33
Hey fellow iOs programmers. I have run into a dilemma trying to complete a tutorial I found online. The problem is that the app is suppose to display a gradient affect to the app but whenever I run the simulator, the piece of code that is suppose to add that gradient affect is not executing. Xcode isn't giving me any errors but the cells are the default white in the simulator. I am a beginner so it might be something easy to fix or see the problem. Any help would be appreciated!
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var toDoItems = [ToDoItem]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.separatorStyle = .None
tableView.rowHeight = 50.0;
if toDoItems.count > 0 {
return
}
toDoItems.append(ToDoItem(text: "Do homework"))
toDoItems.append(ToDoItem(text: "Feed the cat"))
toDoItems.append(ToDoItem(text: "Take out the garbage"))
toDoItems.append(ToDoItem(text: "Learn Swift"))
toDoItems.append(ToDoItem(text: "Get a haircut"))
toDoItems.append(ToDoItem(text: "Fix the laptop"))
toDoItems.append(ToDoItem(text: "Take a shower"))
toDoItems.append(ToDoItem(text: "Work out"))
}
// Tabel view data source
func numberOfSectionsInTableView(tableView: UITableView) ->
Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toDoItems.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell",
forIndexPath: indexPath) as UITableViewCell
let item = toDoItems[indexPath.row]
cell.textLabel?.text = item.text
return cell
}
// Color of cells ** Here is the piece of code that is not working!**
func colorForIndex(index: Int) -> UIColor {
let itemCount = toDoItems.count - 1
let val = (CGFloat(index) / CGFloat(itemCount)) * 0.6
return UIColor(red: 1.0, green: val, blue: 0.0, alpha: 1.0)
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowatIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = colorForIndex(indexPath.row)
}
}
Upvotes: 0
Views: 47
Reputation: 154583
You have a typo in your function name. It should be forRowAtIndexPath
(with a capital A in At):
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowatIndexPath indexPath: NSIndexPath) {
^^^ typo here
cell.backgroundColor = colorForIndex(indexPath.row)
}
Upvotes: 2