Search code examples
iosswiftcellnavigationbar

how to access function properly?


here i want to use this function which i have mention in custom subclass of uiview

this is messageviewcontroller file

import UIKit

    class MessageViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        var MessageData : NSMutableArray!
        @IBOutlet weak var tableviee: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            
            MessageData = NSMutableArray()
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Silvestor Young", profileMessage: "Hi, I want to purchase your service, can you please provide it to me", Messagtime: "10 min ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Welma Simpson", profileMessage: "How much is it cost to repeate it again", Messagtime: "22 min ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Robert Doe", profileMessage: "It was really nice and calm", Messagtime: "1h ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Steve Adams", profileMessage: "I need an extra piece of this can you provide it to me.", Messagtime: "1 h ago"))

            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Alizia Smith", profileMessage: "ok noted thanks", Messagtime: "3 h ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "JW Doe", profileMessage: "Thank you for your inquiry om this can you provide details for your so i can contact you", Messagtime: "3 h ago"))
            
            tableviee.register(UINib(nibName: "MessageMainCell", bundle: nil), forCellReuseIdentifier: "MessageMainCell")
            let messageBar = MessageBar.instanceFromNib() as! MessageBar
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: messageBar)
        }
        
        func getMessageListData(image: String, profileName: String, profileMessage: String, Messagtime: String) -> NSMutableDictionary{
            let data = NSMutableDictionary()
            data["image"] = image
            data["profileName"] = profileName
            data["profileMessage"] = profileMessage
            data["Messagetime"] = Messagtime
            return data
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return MessageData.count
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MessageMainCell", for: indexPath ) as! MessageMainCell
            if let setData = MessageData.object(at: indexPath.row) as? NSDictionary{
                cell.setListdata(data: setData)
            }
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
            let stroryBoard = UIStoryboard(name: "Main", bundle: nil)
            let rootVc = stroryBoard.instantiateViewController(withIdentifier: "MessageDetailViewController") as! MessageDetailViewController
            let backImage = UIImage(named: "backArrowWhite")
            self.navigationController?.navigationBar.backIndicatorImage = backImage
            self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
            self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
            self.navigationController?.navigationBar.tintColor = .black
            self.navigationController?.pushViewController(rootVc, animated: true)
        }
    }

import UIKit

class MessageDetailbar: UIView {

@IBOutlet weak var time: UILabel!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var imageView: UIImageView!



class func instanceFromNib() -> UIView {
    return UINib(nibName: "MessageDetailbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}

func setBarData(data: NSDictionary){
    if let image = data["image"] as? String{
        imageView.image = UIImage(named: image)
    }

    if let Name = data["profileName"] as? String{
        name.text = Name
    }

    if let Time = data["Messagetime"] as? String{
        time.text = Time
    }
}

}

here i have used my customview in viewwillAppear

import UIKit

class MessageDetailViewController: UIViewController {

@IBOutlet weak var tableview: UITableView!

struct data{
    let time: String
    let message: String
    let date: String
    let type: String
}

struct section {
    let date: String
    var array = [data]()
}

let sectionArray = [
    section(date: "yesterday", array: [
            data(time: "3:30 PM", message: "I am looking for your service, can you please give more information on that.", date: "yesterday", type: "sender"),
            data(time: "3:40 PM", message: "Sure i am here to help you", date: "yesterday", type: "receiver")
        ]),
    section(date: "today", array: [
        data(time: "4:40 PM", message: "Ok, I wil contact you on your phone for that.", date: "today", type: "sender"),
        data(time: "5: 30 PM", message: "SO HERRE IS my contact number 9586670629", date: "today", type: "receiver"),
        data(time: "5: 30 PM", message: "OKAY I will call back you within 15 20 minitues", date: "today", type: "sender")
        ])
]

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableview.register(UINib(nibName: "SenderCell", bundle: nil), forCellReuseIdentifier: "SenderCell")
    self.tableview.register(UINib(nibName: "ReceiverCell", bundle: nil), forCellReuseIdentifier: "ReceiverCell")
    self.tableview.register(UINib(nibName: "customHeaderViewCell", bundle: nil), forCellReuseIdentifier: "customHeaderViewCell")
}

override func viewWillAppear(_ animated: Bool) {
    let messageDetailBar = MessageDetailbar.instanceFromNib() as! MessageDetailbar
    self.navigationItem.titleView = messageDetailBar
}

}

extension MessageDetailViewController: UITableViewDelegate, UITableViewDataSource{

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sectionArray[section].array.count
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableview.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if sectionArray[indexPath.section].array[indexPath.row].type == "sender"
        {
            let cell = tableview.dequeueReusableCell(withIdentifier: "SenderCell", for: indexPath) as! SenderCell
            cell.setData(reciverTime: sectionArray[indexPath.section].array[indexPath.row].time, reciverMsg: sectionArray[indexPath.section].array[indexPath.row].message)
            return cell
        }
        else
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ReceiverCell", for: indexPath) as! ReceiverCell
            cell.setData(reciverTime: sectionArray[indexPath.section].array[indexPath.row].time, reciverMsg: sectionArray[indexPath.section].array[indexPath.row].message)
            return cell
        }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()
    let headerCell = tableView.dequeueReusableCell(withIdentifier: "customHeaderViewCell") as! customHeaderViewCell
    if sectionArray[section].date == "yesterday"
        {
            headerCell.labelHeader.text = "yesterday"
        }
    else
        {
            headerCell.labelHeader.text = "today"
        }
    headerView.addSubview(headerCell)
    return headerView
    }
}

but i am not able to use it how can use it proper way

i want to use custom navigation bar when on of the cell is selected [this] cell2 to [this] navigation bar 3


Solution

  • You don't show your MessageDetailViewController or how you're passing the data from the selected index path, but assuming you have something like this:

    class MessageDetailViewController: UIViewController {
    
        public var theData: NSDictionary!
    

    you'll be passing the data in didSelectRowAt like this:

        let stroryBoard = UIStoryboard(name: "Main", bundle: nil)
        let rootVc = stroryBoard.instantiateViewController(withIdentifier: "MessageDetailViewController") as! MessageDetailViewController
        
        // unwrap optional
        guard let setData = MessageData.object(at: indexPath.row) as? NSDictionary else { return }
        
        // pass the data to the Detail controller
        rootVc.theData = setData
        
        self.navigationController?.pushViewController(rootVc, animated: true)
    

    Then, in viewDidLoad() in MessageDetailViewController:

    class MessageDetailViewController: UIViewController {
    
        public var theData: NSDictionary!
        
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // whatever else you're doing to congfigure
            //  this controller
            
            // get an instance of MessageDetailbar
            let messageDetailBar = MessageDetailbar.instanceFromNib() as! MessageDetailbar
            // set the data
            messageDetailBar.setBarData(data: theData)
            // set it as the navigation bar's titleView
            self.navigationItem.titleView = messageDetailBar
    
        }
    
    }
    

    As a side note, override intrinsicContentSize in your MessageDetailbar class so it will fill the available horizontal space:

    class MessageDetailbar: UIView {
        
        @IBOutlet weak var time: UILabel!
        @IBOutlet weak var name: UILabel!
        @IBOutlet weak var imageView: UIImageView!
        
        class func instanceFromNib() -> UIView {
            return UINib(nibName: "MessageDetailbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
        }
        
        func setBarData(data: NSDictionary){
            if let image = data["image"] as? String{
                imageView.image = UIImage(named: image)
            }
            
            if let Name = data["profileName"] as? String{
                name.text = Name
            }
            
            if let Time = data["Messagetime"] as? String{
                time.text = Time
            }
        }
    
        // add this
        override var intrinsicContentSize: CGSize {
            return UIView.layoutFittingExpandedSize
        }
    
    }