您好我试图将didSelectRowAtIndexPath
func中的变量传递给另一个视图控制器:
class MessagesViewController: UIViewController, UITableViewDelegate {
@IBOutlet var messagesTableView: UITableView!
@IBOutlet var invitesButton: UIButton!
var messagesTableViewDataSource: MessagesTableViewDatasource = MessagesTableViewDatasource()
override func viewDidLoad() {
super.viewDidLoad()
messagesTableView.dataSource = self.messagesTableViewDataSource
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
let titleView = NavTitleView.init(frame: CGRect(x: 0, y: 0, width: 122, height: 32))
titleView.title = "Messages"
self.navigationItem.titleView = titleView
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell: MessageCell = tableView.cellForRowAtIndexPath(indexPath) as! MessageCell
let viewController = storyboard?.instantiateViewControllerWithIdentifier("ConversationViewController") as! ConversationViewController
viewController.conversationUUID = cell.conversationUUID
viewController.conversationProfileImage = cell.senderProfileImage
}
@IBAction func invitesButtonWasPressed() {
performSegueWithIdentifier("segueToInvites", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这里是构建单元格的地方:
class MessagesTableViewDatasource: NSObject, UITableViewDataSource {
var messages: [InboundMessage] = []
func createMessagesArray(message: InboundMessage) {
self.messages.append(message)
}
func loadMessages() -> [InboundMessage] {
if self.messages.count > 0 {
return self.messages
} else {
InboundMessage.all { messages in
messages.forEach(self.createMessagesArray)
}
return self.messages
}
}
func createTableViewFooter(tableView: UITableView) {
let footer = UILabel(frame: CGRectMake(0, 0, tableView.bounds.size.width - 100, tableView.bounds.size.height))
footer.text = "You have no more messages."
footer.textAlignment = .Center
footer.textColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25)
footer.font = Style.smallFont
footer.sizeToFit()
tableView.tableFooterView = footer
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
loadMessages()
createTableViewFooter(tableView)
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
let cell: MessageCell = tableView.dequeueReusableCellWithIdentifier("MessageCell") as! MessageCell
cell.senderName.text = messages[indexPath.row].senderName
cell.senderText.text = messages[indexPath.row].senderText
cell.timeMessageReceived.text = dateFormatter.stringFromDate(messages[indexPath.row].timeAndDateMessageReceived)
cell.conversationUUID = messages[indexPath.row].conversationUUID
if (messages[indexPath.row].senderProfileImageName != nil) {
cell.senderProfileImage!.image = UIImage(named: messages[indexPath.row].senderProfileImageName)
}
if (messages[indexPath.row].hasBeenRead == false) {
cell.unOpenedImage!.image = UIImage(named: "RedDot")
}
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
loadMessages()
if self.messages.count == 0 {
let noMessagesLabel = UILabel(frame: CGRectMake(0, 0, tableView.bounds.size.width, tableView.bounds.size.height))
noMessagesLabel.text = "You have no messages at the moment."
noMessagesLabel.textAlignment = .Center
noMessagesLabel.sizeToFit()
tableView.backgroundView = noMessagesLabel
tableView.separatorStyle = .None
return 0
}
return 1
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
}
但是当我回到我的视图控制器时,值为零,我不明白为什么。这是传递给它的视图控制器:
class ConversationViewController: UIViewController {
@IBOutlet var conversationTableView: UITableView!
var conversationTableViewDataSource: ConversationTableViewDataSource = ConversationTableViewDataSource()
var conversationUUID: NSUUID!
var conversationProfileImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
conversationTableView.dataSource = self.conversationTableViewDataSource
conversationTableViewDataSource.conversationUUID = self.conversationUUID
conversationTableViewDataSource.conversationProfileImage = self.conversationProfileImage
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
// let titleView = NavTitleView.init(frame: CGRect(x: 0, y: 0, width: 122, height: 32))
// titleView.title = "Messages"
// self.navigationItem.titleView = titleView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我敢肯定,我错过了一些简单的东西,但由于我对这一切都很陌生,所以我无法在谷歌搜索后找到它并且希望能有所了解。
由于
答案 0 :(得分:1)
您需要在prepareForSegue
prepareForSegue
方法添加MessagesViewController
方法
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueToConversation") {
let destVC = segue.destinationViewController as! ConversationViewController
let cell = sender as! MessageCell
destVC.conversationUUID = cell.conversationUUID
destVC.passImage = cell.senderProfileImage.image
}
}
注意:您无法直接将图片分配给UIImageView
对象conversationProfileImage
,因为它尚未初始化,因此在UIImage
的实例上创建{ {1}}名称为ConversationViewController
,如我在回答中所写,然后使用passImage
中的图像对象将图像分配给viewDidLoad
。