我的应用中有多个tableview,当用户单击一个单元格时,一个视图控制器上的特定图像。因为我有很多表视图,所以我希望在一个文件中包含多个代码。我现在的代码打开了第一个"心脏" tableview并可以单击一个单元格来查看图片,但对于其他人,应用程序崩溃;即使代码编译。我无法弄清楚原因,并且我确定它很简单。我的代码如下:
import UIKit
import GoogleMobileAds
///You are using this class a UIViewCOntroller in storyBoard bbut hadnt provided **tablelist: UIViewController**
///This is Necessary in case when you are going to assign it to a UIVIewController
class adultprotocols: UIViewController, UITableViewDataSource, UITableViewDelegate, GADBannerViewDelegate {
var bannerView: GADBannerView!
override func viewDidLoad(){
super.viewDidLoad()
self.navigationController?.navigationBar.barTintColor = UIColor.black
let textAttributes = [NSForegroundColorAttributeName:UIColor.red]
navigationController?.navigationBar.titleTextAttributes = textAttributes
///You need to Sert datasource and Delegate as its not a TableViewController its just a Normal TableView
self.cardiacTableView.dataSource = self
self.cardiacTableView.delegate = self
self.view.backgroundColor = UIColor.black
//---------ADS-------------------------------------------
// In this case, we instantiate the banner with desired ad size.
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
addBannerViewToView(bannerView)
}
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.rootViewController = self
bannerView.load(GADRequest())
}
//--------------------------LIST Specs-------------------------------------------------------//
let cardiac = ["one", "twoasdg", "Cowasdasd", "Mulvadasdal"]
let respiratory = ["one", "AsdaSD", "asdasdadad"]
let trauma = ["one", "two", "three"]
let ams = ["one","two"]
///You did not connected a outlet of tableView
///as its not a tableViewController just a TableView so it should be Connected
@IBOutlet weak var animalTableView: UITableView!
@IBOutlet weak var cardiacTableView: UITableView!
@IBOutlet weak var respiratoryTableView: UITableView!
///Set elements in cell
func tableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "adultcardiaclist"/*Identifier*/, for: indexPath)
cell.textLabel?.text = cardiac[indexPath.row]
// FONT STYLE
cell.textLabel?.textColor = UIColor.white
cell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 30.0)
cell.textLabel?.textAlignment = .center
self.cardiacTableView.backgroundColor = UIColor.clear
cell.textLabel?.backgroundColor = UIColor.black
return cell
}
func resptableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "adultrespiratorylist"/*Identifier*/, for: indexPath)
cell.textLabel?.text = respiratory[indexPath.row]
// FONT STYLE
cell.textLabel?.textColor = UIColor.white
cell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 30.0)
cell.textLabel?.textAlignment = .center
self.respiratoryTableView.backgroundColor = UIColor.clear
cell.textLabel?.backgroundColor = UIColor.black
return cell
}
func traumatableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "adulttraumalist"/*Identifier*/, for: indexPath)
cell.textLabel?.text = cardiac[indexPath.row]
// FONT STYLE
cell.textLabel?.textColor = UIColor.white
cell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 30.0)
cell.textLabel?.textAlignment = .center
self.cardiacTableView.backgroundColor = UIColor.clear
cell.textLabel?.backgroundColor = UIColor.black
return cell
}
func amstableView( _ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "adultamslist"/*Identifier*/, for: indexPath)
cell.textLabel?.text = cardiac[indexPath.row]
// FONT STYLE
cell.textLabel?.textColor = UIColor.white
cell.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 30.0)
cell.textLabel?.textAlignment = .center
self.cardiacTableView.backgroundColor = UIColor.clear
cell.textLabel?.backgroundColor = UIColor.black
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return cardiac.count
}
func resptableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return respiratory.count
}
func traumatableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return trauma.count
}
func amstableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return ams.count
}
//--------------------------Cardiac List--------------------
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
///Right way here
///You can easily manage using this
let Vc = self.storyboard?.instantiateViewController(withIdentifier: "imageViewController") as! imageViewController
///Here you have written four Animal names in Array
///So There is going to four case 0,1,2,3 and a default case
switch indexPath.row
{
case 0:
Vc.passedImage = UIImage.init(named: "image1")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
case 1:
Vc.passedImage = UIImage.init(named: "image2")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
case 2:
Vc.passedImage = UIImage.init(named: "image3")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
case 3:
Vc.passedImage = UIImage.init(named: "image1")!
self.navigationController?.pushViewController(Vc, animated: true)
break;
default:
Vc.passedImage = UIImage.init(named: "image2")!
self.navigationController?.pushViewController(Vc, animated: true)
}
}
func resptableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
///Right way here
///You can easily manage using this
let Bv = self.storyboard?.instantiateViewController(withIdentifier: "imageViewController") as! imageViewController
///Here you have written four Animal names in Array
///So There is going to four case 0,1,2,3 and a default case
switch indexPath.row
{
case 0:
Bv.passedImage = UIImage.init(named: "image1")!
self.navigationController?.pushViewController(Bv, animated: true)
break;
case 1:
Bv.passedImage = UIImage.init(named: "image2")!
self.navigationController?.pushViewController(Bv, animated: true)
break;
case 2:
Bv.passedImage = UIImage.init(named: "image3")!
self.navigationController?.pushViewController(Bv, animated: true)
break;
case 3:
Bv.passedImage = UIImage.init(named: "image1")!
self.navigationController?.pushViewController(Bv, animated: true)
break;
default:
Bv.passedImage = UIImage.init(named: "image2")!
self.navigationController?.pushViewController(Bv, animated: true)
}
}
我使用" func tableview" " func resptableview"不正确?
答案 0 :(得分:0)
你的问题太模糊了。什么行崩溃,有什么错误信息?
我的猜测是你因使用force unwrap(!
)运算符而崩溃。如果没有"我称之为"崩溃运营商,因为它的作用。
您应该将目的地imageViewController
' passedImage
属性设为Optional
,然后摆脱强制解包。然后编写imageViewController,以便处理图像视图为零的情况。
作为初学者,你应该完全避免使用强制解包操作符。我建议使用它的唯一一次是Xcode设置一个IBOutlet
作为一个强制解包的可选项,因为你想知道你是否有一个断开的出口链接,并且当它没有时崩溃让你早点了解问题。
同时避免as!
强制施放。这是另一种情况,如果失败就会崩溃。习惯使用guard
语句或if let
可选绑定。