Ios 项目中的颜色代码执行不正确
嘿,iOs程序员们。我在网上找到了一本教程,但在完成这本教程时遇到了两难的境地。问题是,应用程序被假定为向应用程序显示渐变效果,但每当我运行模拟器时,假定添加渐变效果的代码段都不会执行。Xcode没有给我任何错误,但是在模拟器中单元格是默认的白色。我是一个初学者,所以它可能是一些容易修复或看到的问题。任何帮助都将不胜感激Ios 项目中的颜色代码执行不正确,ios,swift,xcode6,Ios,Swift,Xcode6,嘿,iOs程序员们。我在网上找到了一本教程,但在完成这本教程时遇到了两难的境地。问题是,应用程序被假定为向应用程序显示渐变效果,但每当我运行模拟器时,假定添加渐变效果的代码段都不会执行。Xcode没有给我任何错误,但是在模拟器中单元格是默认的白色。我是一个初学者,所以它可能是一些容易修复或看到的问题。任何帮助都将不胜感激 import UIKit class ViewController: UIViewController, UITableViewDataSource,
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
}
//单元格颜色
**这是一段不起作用的代码!**
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)
}
}
函数名中有一个输入错误。它应该是
for rowatinexpath
(大写字母a位于):
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowatIndexPath indexPath: NSIndexPath) {
^^^ typo here
cell.backgroundColor = colorForIndex(indexPath.row)
}