【发布时间】: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没有被调用。另一方面,您是否考虑扩展UIViewController或UITableViewController? -
@rmaddy - 我尝试使用 UIViewController 但因为我的自定义 viewController 是从另一个 ViewController 的准备 segue 函数触发的,所以我必须添加 UINavigatorController 否则它会出错。
标签: ios swift uitableview uinavigationcontroller