Ios 如何在tableView中将文本指定给单元格?
我正在swift中创建任务列表应用程序,代码为:Ios 如何在tableView中将文本指定给单元格?,ios,swift,uitableview,Ios,Swift,Uitableview,我正在swift中创建任务列表应用程序,代码为: import UIKit class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource { var tableView : UITableView! var textField : UITextField! var tableViewData = ["My Text
import UIKit
class ViewController: UIViewController, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
var tableView : UITableView!
var textField : UITextField!
var tableViewData = ["My Text 1", "My Text 2"]
override func viewDidLoad() {
super.viewDidLoad()
//Set up textField
self.textField = UITextField(frame: CGRectMake(0, 0, self.view.bounds.size.width, 100))
self.textField.backgroundColor = UIColor.redColor()
self.view.addSubview(self.textField)
//Set up table view
self.tableView = UITableView(frame: CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height-100), style: UITableViewStyle.Plain)
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myCell")
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
}
//TableView Data source Delegate
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return tableViewData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let myNewCell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
//Here is the error
myNewCell.text = self.tableViewData[indexPath.row]
return myNewCell
}
}
myNewCell.text = self.tableViewData[indexPath.row]
错误是:
'text' is unavailable:APIs deprecated as of iOS 7 and earlier are unavailable in Swift
myNewCell.text = self.tableViewData[indexPath.row]
我在教程中这样做,那个人使用了xCode 6.0,我有xCode 6.1测试版,我认为这是问题所在,因为我使用的是测试版。
有人能告诉我这个错误是什么吗?我是swift的新手,有人能告诉我怎么做吗 您需要使用
myNewCell.textLabel?.text = self.tableViewData[indexPath.row]
myNewCell.text = self.tableViewData[indexPath.row]
myNewCell.textLabel?.text = self.tableViewData[indexPath.row]
过去是:
myNewCell.text = self.tableViewData[indexPath.row]
但是它们将text属性嵌入到textlab属性中。您需要使用
myNewCell.textLabel?.text = self.tableViewData[indexPath.row]
myNewCell.text = self.tableViewData[indexPath.row]
myNewCell.textLabel?.text = self.tableViewData[indexPath.row]
过去是:
myNewCell.text = self.tableViewData[indexPath.row]
但是他们将text属性嵌入到textLabel属性中。使用这个
myNewCell.text = self.tableViewData[indexPath.row]
myNewCell.textLabel?.text = self.tableViewData[indexPath.row]
用这个
myNewCell.text = self.tableViewData[indexPath.row]
myNewCell.textLabel?.text = self.tableViewData[indexPath.row]
您需要调用textLabel,textLabel是可选的,因此您需要将其展开:
myNewCell.text = self.tableViewData[indexPath.row]
if let textLabel = myNewCell.textLabel {
textLabel.text = self.tableViewData[indexPath.row]
}
或
myNewCell.text = self.tableViewData[indexPath.row]
您需要调用textLabel,textLabel是可选的,因此您需要将其展开:
myNewCell.text = self.tableViewData[indexPath.row]
if let textLabel = myNewCell.textLabel {
textLabel.text = self.tableViewData[indexPath.row]
}
或
myNewCell.text = self.tableViewData[indexPath.row]