【问题标题】:Custom TableView functions are not getting called?自定义 TableView 函数没有被调用?
【发布时间】:2015-12-14 22:34:23
【问题描述】:

在我的 main.Storyboard 上,我有一个 TableViewController,它使用 TableViewController.swift 设置为自定义类。

swift 文件定义了所有的 tableview 函数,并连接了 UITableView 的 @IBOutlet。定义的类是 UINavigationController、UITableViewDelegate。这个 viewController 是通过 prepareForSegue 函数从 secondViewController 调用的。

我还为 UITableViewCell 中的标签创建了 CustomCell.swift 类 UITableViewCell 和所有 @IBOutlet,这些标签已设置为 customCell 类。

我无法粘贴所有代码,但如果您需要查看任何特定代码,请告诉我,我很乐意发布。

构建成功,应用程序运行,但 tableviewcells 没有显示,并且没有调用任何 tableview 函数。我看到 2 个 Flash 动画屏幕 - 表明 tableviewcell 可能有 2 个视图 - 但不知道我应该检查哪里?

//下面是触发TableViewController的segue函数

        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        //print("In prepareswgue: ",segue, " ",sender)
        if(segue.identifier == "resultSegue")
        {
            let nav = segue.destinationViewController as! UINavigationController
            let svc = nav.topViewController as! TableViewController
            svc.serialNo = self.TSSerialNoField.text
    }
}


//Below is the custom TableVIewController class code
class TableViewController: UINavigationController,UITableViewDataSource, UITableViewDelegate
{
    var serialNo:String!
    var ashHardwareData: NSMutableArray!

    @IBOutlet var ResultTableView: UITableView!
    //@IBOutlet weak var LogCaseButton: UIButton!


    @IBOutlet weak var TypeResultLabel: UILabel!



    override func viewDidLoad() {
        super.viewDidLoad()

        self.ResultTableView?.allowsSelectionDuringEditing = true
        self.ResultTableView?.delegate = self
        ResultTableView?.dataSource = self
    }


    override func viewDidAppear(animated: Bool) {
        self.getHardwareData(serialNo.uppercaseString)
    }

    /*override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
    }*/

    func getHardwareData(serialno:String)
    {
        ashHardwareData = NSMutableArray()
        ashHardwareData = ModelManager.getInstance().getHardwareData(serialno)
        ResultTableView?.reloadData()
    }
    //TableView Delegate Methods
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        print("In height func")
        return 50
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print(ashHardwareData.count)
        return ashHardwareData.count
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell:ResultsCell = tableView.dequeueReusableCellWithIdentifier("results", forIndexPath: indexPath) as! ResultsCell
        let hardware:HardwareInfo = ashHardwareData.objectAtIndex(indexPath.row) as! HardwareInfo
        let contract:ContractInfo = ashHardwareData.objectAtIndex(indexPath.row) as! ContractInfo
        cell.SNOLabel.text = "Serial N0: \(hardware.SerialNo)"
        cell.ContractIDLabel.text = "Contract ID: \(contract.ContractID)"
        cell.OrgLabel.text = "Organisation: \(hardware.Organisation)"
        cell.ModelLabel.text = "Model: \(hardware.Model)"
        if(contract.DaystoExpiry > 0) {
            cell.TypeLabel.text = "Contract Type: Active"
            self.TypeResultLabel.hidden = false
            self.TypeResultLabel.text = "To log a technical case for the Hardware please click on Log Technical Case button."
            cell.LogCaseButton.hidden = false
            cell.LogCaseButton.tag = indexPath.row
        }
        else {
            cell.TypeLabel.text = "Contract Type: Expired"
            cell.LogCaseButton.hidden = true
            self.TypeResultLabel.hidden = false
            self.TypeResultLabel.text = "Support Contract for the hardware expired.  Please contact Sales team to renew the contract."
        }
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


//here is the code for the ResultsCell custom UITableViewCell

    import Foundation
import UIKit

class ResultsCell: UITableViewCell {


    @IBOutlet weak var SNOLabel: UILabel!

    @IBOutlet weak var LogCaseButton: UIButton!
    @IBOutlet weak var ContractIDLabel: UILabel!
    @IBOutlet weak var OrgLabel: UILabel!
    @IBOutlet weak var TypeLabel: UILabel!
    @IBOutlet weak var ModelLabel: UILabel!


    override func awakeFromNib() {
        super.awakeFromNib()
    }

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

【问题讨论】:

  • 您需要发布一些代码,否则很难判断问题所在。
  • 我已经用代码修改了问题。如果您需要更多信息,请告诉我。感谢您的快速回复
  • 为什么你的TableViewController 扩展UINavigationController?为什么不UIViewController 甚至UITableViewController
  • @Ashley 好像你不符合UITableViewDataSource 协议,结果,UITableViewDelegate 没有被调用。另一方面,您是否考虑扩展UIViewControllerUITableViewController
  • @rmaddy - 我尝试使用 UIViewController 但因为我的自定义 viewController 是从另一个 ViewController 的准备 segue 函数触发的,所以我必须添加 UINavigatorController 否则它会出错。

标签: ios swift uitableview uinavigationcontroller


【解决方案1】:

您是否在故事板或view didload 中配置了ResultsCell。检查一次。如果没有尝试将以下代码添加到您的view didload

[self.tableView registerNib:[UINib nibWithNibName:@"ResultsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ResultsCell"];

【讨论】:

  • 如果我在 viewDidLoad 函数中声明您的代码,我会收到错误消息。我将 Swift 与 xcode 7.1.1 一起使用。它要求我放一个 ;在:forCellReuseIdentifier 之后。我有 ResultsCell.swift 文件,声明了要在 tableVIewCell 中显示的所有标签
  • 试试这个,self.tableView.registerClass(ResultsCell.self, forCellReuseIdentifier: "Cell")
【解决方案2】:

我花了相当多的时间试图解决这个问题并决定不使用 tableviewcell,而是创建了自定义 viewControllers 并定义了标签来显示来自 DB 搜索的结果(工作正常)并使用执行 segue 函数在视图控制器之间传递值。所以现在我的应用程序按照我想要的方式工作 - 从编程方面来说可能并不理想,但由于时间限制,我不得不让它工作。 我的应用程序是带有第一个和第二个 ViewControllers 的选项卡式应用程序。 FirstViewController 将数据添加到 SQLLite DB,而 SecondViewController 搜索数据并显示结果。为了显示结果,我创建了一个自定义视图控制器,其中包含我想要显示的数据的标签,并将数据库结果中的所有数据传递给 SecondViewController 函数,并用数据更新标签。只要我得到我想要的结果,我就很高兴。如果有必要,我会重新访问它以进行改进。感谢所有回复解决方案和建议的人。这是一次很好的学习经历:)

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    相关资源
    最近更新 更多