gpt4 book ai didi

ios - Swift 文件包含一个 Imageview 的多个 TableView 代码

转载 作者:行者123 更新时间:2023-11-30 11:45:23 25 4
gpt4 key购买 nike

我的应用程序中有多个表格 View ,当用户单击某个单元格时,一个 View Controller 上会显示特定图像。因为我有很多 TableView ,所以我想将多个 TableView 的代码放在一个文件中。我现在的代码打开第一个“心脏”表格 View ,并且可以单击单元格来查看图片,但对于其他单元格,应用程序崩溃;即使代码编译。我不明白为什么,但我确信这很简单。我的代码如下:

    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”?

最佳答案

你的问题太模糊了。哪一行崩溃了,并显示什么错误消息?

我的猜测是,您由于使用强制展开(!)运算符而崩溃。我称其为“crash if nil”运算符,因为这就是它的作用。

您应该将目标 imageViewControllerpassedImage 属性设置为 Optional,并摆脱强制展开。然后编写你的 imageViewController 以便它处理 ImageView 为零的情况。

作为初学者,您应该完全避免使用强制展开运算符。我建议使用它的唯一一次是当 Xcode 将 IBOutlet 设置为强制解包可选时,因为您想立即知道您是否有一个损坏的 socket 链接,并且当它不让时崩溃你很早就知道这个问题了。

还要避免 as! 强制转换。这是另一种如果失败就会崩溃的情况。习惯使用 guard 语句或 if let 可选绑定(bind)。

关于ios - Swift 文件包含一个 Imageview 的多个 TableView 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48889824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com