gpt4 book ai didi

swift - 第二级 UITableView 无法在 SWIFT 中工作

转载 作者:行者123 更新时间:2023-11-30 10:28:04 25 4
gpt4 key购买 nike

我有一个像这样的 UITableView:

import UIKit
import SDWebImage

class ChatViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var ConversationList: UITableView!
let apiService = APIService()
var conversations: [Conversation] = []
var selectedConversation: Int?



override func viewDidLoad() {
super.viewDidLoad()
ConversationList.separatorStyle = .none
ConversationList.dataSource = self
ConversationList.delegate = self

// load Conversations
self.apiService.getConversations(completion: {result in
switch result {
case .success(let conversations):
DispatchQueue.main.async {
print("NUMBER OF CONVERSATIONS: ", conversations.count)
self.conversations = conversations
self.ConversationList.reloadData()
}
case .failure(let error):
print("An error occured \(error.localizedDescription)")
}
})
}

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



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChatViewCell", for: indexPath) as! ChatViewCellController

cell.UserName.text = self.conversations[indexPath[1]].participants![1].username
let imgURL = URL(string: self.conversations[indexPath[1]].participants![1].profileimage!)
cell.UserLogo.sd_setImage(with: imgURL, placeholderImage: UIImage(named: "icon.turq.png"))
return cell
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected row number ", indexPath)
self.selectedConversation = indexPath[1]
self.performSegue(withIdentifier: "ChatToChatDetail", sender: self)
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! ChatDetailViewController
destination.name = self.conversations[self.selectedConversation!].participants![1].username!
destination.img = self.conversations[self.selectedConversation!].participants![1].profileimage!
}


}

UITableViewCell 位于一个单独的简单类中,如下所示:

import UIKit

class ChatViewCellController: UITableViewCell {

@IBOutlet weak var UserLogo: UIImageView!
@IBOutlet weak var UserName: UILabel!



override func awakeFromNib() {
super.awakeFromNib()
self.UserLogo.clipsToBounds = true
self.UserLogo.layer.cornerRadius = self.UserLogo.frame.size.width / 2
}



override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

如上所示,选择 UITableView 中的特定单元格(即对话)通过 segue 加载另一个 UIViewController。然后,该 UIViewController 包含另一个 UITableView:

import UIKit
import SDWebImage

class ChatDetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var UserLogo: UIImageView!
@IBOutlet weak var UserName: UILabel!
@IBOutlet weak var MessageList: UITableView!
var name: String = ""
var img: String = ""
let apiService = APIService()
var messages: [ChatMessage] = []


override func viewDidLoad() {
super.viewDidLoad()
// prepare header section:
self.MessageList.separatorStyle = .none
let imgURL = URL(string: self.img)
self.UserLogo.sd_setImage(with: imgURL, placeholderImage: UIImage(named: "icon.turq.png"))
self.UserLogo.clipsToBounds = true
self.UserLogo.layer.cornerRadius = self.UserLogo.frame.size.width / 2
self.UserName.text = self.name

// TODO: load messages

self.MessageList.reloadData()
}



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MessageViewCell", for: indexPath) as! ChatMessageViewCellController
cell.ChatMessageText.text = "foo"

return cell
}
}

上面的 UITableView 的 UITableViewCell 再次位于一个单独的简单类中:

import UIKit

class ChatMessageViewCellController: UITableViewCell {

@IBOutlet weak var ChatMessageBubble: UIView!
@IBOutlet weak var ChatMessageText: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
print("I'm awake")
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

在某个时刻之前一切都运行良好。第一个 TableView 加载并显示所有聊天。单击聊天会加载带有标题部分的新 View ,在屏幕顶部显示聊天伙伴的 Logo 和名称,并在其下方显示表格。然而,该表不包含任何内容。据我了解,使用这段代码,它应该显示 10 行“foo”,并向控制台打印 10 次“我醒了”。

我在这里遗漏了什么或做错了什么?

最佳答案

在第二个 ViewController 中,您没有设置 UITableViewDelegateUITableViewDataSource。您需要添加到 viewDidLoad 中:

MessageList.delegate = self
MessageList.dataSource = self

额外的奖励,以便将来在第一个 View Controller 中进行更改 self.selectedConversation = indexPath[1] 以防止出现错误 - 它始终会采用第二个模型,即使您只有一个模型。

此外,尽量避免属性使用大写,例如消息列表 -> 消息列表。

关于swift - 第二级 UITableView 无法在 SWIFT 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59715280/

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