Ios 如何在tableview中插入变量?
我正在为课程开发一个简单的乘法应用程序。它有一个滑块来选择1到20之间的数字。其想法是创建一个“时间表”,列出从滑块中选择的任何数字的前50项。我收到的错误消息是“ViewController.Type”没有名为“乘数”的成员。 谢谢你的帮助Ios 如何在tableview中插入变量?,ios,uitableview,swift,Ios,Uitableview,Swift,我正在为课程开发一个简单的乘法应用程序。它有一个滑块来选择1到20之间的数字。其想法是创建一个“时间表”,列出从滑块中选择的任何数字的前50项。我收到的错误消息是“ViewController.Type”没有名为“乘数”的成员。 谢谢你的帮助 import UIKit class ViewController: UIViewController, UITableViewDelegate { @IBOutlet weak var sliderValue: UISlider! @
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.
}
}
将
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"]
}
待办事项
...
@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)"]
...
在viewDidLoad()中设置cellContent(单元格内容)@Arbiutr这真的会有什么不同吗?我面前没有我的mac电脑,只是将它移动到函数中,如果仍然出现错误,请尝试。我认为您不能使用函数外的其他变量设置变量。@Arbitor您认为这样做可以做什么?
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
}
}