我正在开发一个简单的乘法应用程序来完成课程。它有一个滑块可以选择1到20之间的数字。我们的想法是创建一个'时间表',列出从滑块中选择的任何数字的前50个项目。我得到的错误信息是'ViewController.Type'没有名为'multiplier'的成员。 谢谢你的帮助。
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var sliderValue: UISlider!
@IBAction func sliderMoved(sender: AnyObject) {
println(sliderValue)
}
var multiplier = 1
var cellContent = ["\(multiplier) times 1 is 1", "1 times 2 is 2", "1 times 3 is 3"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
}
感谢您花时间回答我的问题。这些建议对我没有帮助。这是我的导师的方法:
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
@IBOutlet weak var sliderValue: UISlider!
@IBAction func sliderMoved(sender: AnyObject) {
table.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
let timesTable = Int(sliderValue.value * 20)
cell.textLabel?.text = String(timesTable * (indexPath.row + 1))
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
将cellContent
设为:
var cellContent: [String]!
然后在viewDidLoad
上初始化它:
override func viewDidLoad() {
super.viewDidLoad()
cellContent = ["\(multiplier) times 1 is 1", "1 times 2 is 2", "1 times 3 is 3"]
}
答案 1 :(得分:1)
在你的情况下,格式化可以是一个简单的替换字符串
...
@IBAction func sliderMoved(sender: AnyObject) {
multiplier = sliderValue.value; //TODO find right property to save
myTable.reloadData() //reload the table
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
//format it before setting it to the table
//TODO find right method to help you here
var str = cellContent[indexPath.row]
str.stringByReplacingOccurancesOf("(multipler)", NSString("%d", multiple))
str.stringByReplacingOccurancesOf("(result)", NSString("%d", multiple*indexPath.row+1))
cell.textLabel?.text = str
return cell
}
let cellContent = ["(multiplier) times 1 is (result)", "(multiplier) times (result) is 2", "(multiplier) times 3 is (result)"]
...
答案 2 :(得分:0)
var multiplier = 1
var cellContent = ["\(multiplier) times 1 is 1", "1 times 2 is 2", "1 times 3 is 3"]
class ViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var sliderValue: UISlider!
@IBAction func sliderMoved(sender: AnyObject) {
println(sliderValue)
}
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = cellContent[indexPath.row]
return cell
}
}