微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 viewDidLoad 上调用 tableView?

如何解决如何在 viewDidLoad 上调用 tableView?

我是 iOS 新手,正在尝试从 selectedContact 输出数据。它适用于 addressLabel.text 但我不知道如何在表格视图中调用它?

class ContactDetailsVC: UITableViewController {

    @IBOutlet var contactDetailsTableView: UITableView!
    
    @IBOutlet var addressLabel: UILabel!
    
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    var persons = [Persons]()
    
    var selectedContact: Persons? {
        didSet {
            getContactDetails()
        }
    }
            
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "Contact Details"
        navigationController?.navigationBar.prefersLargeTitles = true
                
        contactDetailsTableView = selectedContact?. // <---- this line
        
        addressLabel.text = ("Address \(selectedContact?.address ?? "")")            
    }
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {               
       return 5               
    }
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "contactDetailsCell",for: indexPath)
        
        cell.textLabel?.text = persons[indexPath.row].firstName! + "\n"
                             + persons[indexPath.row].lastName! + "\n"
                             + persons[indexPath.row].company! + "\n"
                             + persons[indexPath.row].phone! + "\n"
                             + persons[indexPath.row].email! + "\n"
        
        return cell
    }
}

解决方法

您应该使用以下委托方法:

func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    let selectedPerson = persons[indexPath.row]
    // Here you can know what person was selected
}

,

感谢大家提供的所有线索。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。