gpt4 book ai didi

ios - 代码有问题,需要帮助

转载 作者:行者123 更新时间:2023-11-30 14:05:28 24 4
gpt4 key购买 nike

我正在尝试使用 parse 构建一个消息应用程序,并且我刚刚开始编写一些代码。但它在 Xcode 6.4 中运行存在一些问题。有人可以帮忙吗。我的类(class)部分也遇到问题。我是初学者。

@IBOutlet weak var messageTableView: UITableView!

var messageArray: [String] = [String]()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let testObject = PFObject(className: "TestObject")
testObject["foo"] = "bar"
testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
println("Object has been saved.")
}
self.messageTableView.delegate = self
self.messageTableView.dataSource = self

messageArray.append("Test 1")
messageArray.append("test 2")
messageArray.append("test 3")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


let cell = self.messageTableView.dequeueReusableCellWithIdentifier("MessageCell") as! UITableViewCell

cell.textLabel?.text = self.messageArray[indexPath.row]

return cell
}

func tableview(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

return messageArray.count
}
}

最佳答案

我认为您想将 messageArray 显示到 tableView 中,但您的委托(delegate)方法不符合 UITableViewDataSource 协议(protocol),因此请将您的方法替换为以下方法:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.messageTableView.dequeueReusableCellWithIdentifier("MessageCell") as! UITableViewCell

cell.textLabel?.text = self.messageArray[indexPath.row]

return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}

您的完整代码将是:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var messageTableView: UITableView!

var messageArray: [String] = [String]()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.messageTableView.delegate = self
self.messageTableView.dataSource = self

messageArray.append("Test 1")
messageArray.append("test 2")
messageArray.append("test 3")
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.messageTableView.dequeueReusableCellWithIdentifier("MessageCell") as! UITableViewCell

cell.textLabel?.text = self.messageArray[indexPath.row]

return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageArray.count
}

}

HERE是您的示例项目。

关于ios - 代码有问题,需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com