extension VoiceController: UITableViewDataSource, UITableViewDelegate {
public func tableView(_ chatHistory: UITableView, numberOfRowsInSection section: Int) -> Int {
return userMessagesData.count
}
public func tableView(_ chatHistory: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
func userMessage() -> UITableViewCell {
let userCell = chatHistory.dequeueReusableCell(withIdentifier: "userMessage")! as! UITableViewCell
userCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
userCell.textLabel!.numberOfLines = 0
userCell.textLabel!.lineBreakMode = .byWordWrapping
userCell.textLabel!.text = userMessagesData[indexPath.row]
userCell.textLabel!.textAlignment = .right
return userCell;
}
func botMessage() -> UITableViewCell {
let botCell = chatHistory.dequeueReusableCell(withIdentifier: "botMessage")! as! UITableViewCell
botCell.textLabel!.textColor = UIColor(red:1.00, green:0.56, blue:0.25, alpha:1.0)
botCell.textLabel!.numberOfLines = 0
botCell.textLabel!.lineBreakMode = .byWordWrapping
botCell.textLabel!.text = botMessagesData[indexPath.row]
botCell.textLabel!.textAlignment = .left
return botCell;
}
}
}
那是我的代码。我需要为此代码做一些工作。如果我将删除其中一个函数,那将无法理解,如何更改我的代码工作。请帮忙
你需要
public func tableView(_ chatHistory: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = arr[indexPath.row]
if item.isUser {
let userCell = chatHistory.dequeueReusableCell(withIdentifier: "userMessage")! as UITableViewCell
userCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
userCell.textLabel!.numberOfLines = 0
userCell.textLabel!.lineBreakMode = .byWordWrapping
userCell.textLabel!.text = item.messsage
userCell.textLabel!.textAlignment = .right
return userCell
}
else {
let botCell = chatHistory.dequeueReusableCell(withIdentifier: "botMessage")! as UITableViewCell
botCell.textLabel!.textColor = UIColor(red:0.00, green:0.33, blue:0.62, alpha:1.0)
botCell.textLabel!.numberOfLines = 0
botCell.textLabel!.lineBreakMode = .byWordWrapping
botCell.textLabel!.text = item.message
botCell.textLabel!.textAlignment = .left
return botCell
}
}
struct Item {
let isUser:Bool
let message:String
}
哪里arr
var arr = [Item]()
您需要返回在函数中声明的相同返回类型,
func botMessage() -> Void //returns void
当你回来一个UITableViewCell
。用Void
取代UITableViewCell
。
另外我注意到你有子函数返回Int
删除该函数是不必要的。