gpt4 book ai didi

ios - 在不使用 XIB CELL 的情况下使用 swift 4.0 中的段在一个 View Controller 中获取两个 TableView

转载 作者:行者123 更新时间:2023-11-28 20:59:19 25 4
gpt4 key购买 nike

我有两个 TableView 和一个段控件在第一个段点击我应该得到第一个表和第二个段点击我应该在运行应用程序时得到第二个 TableView 我得到了这样的异常错误

(Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value)

enter image description here

我的 ViewController 代码:

import UIKit

class DemoTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
struct structOne {
let cell : Int
let one : String
}

struct structTwo {
let cell : Int
let two : String
}

var arrayOne = [structOne]()
var arrayTwo = [structTwo]()
@IBOutlet weak var tableOne: UITableView!
@IBOutlet weak var segmentDemo: UISegmentedControl!

@IBOutlet weak var tableTwo: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
arrayOne = [structOne(cell: 0, one: "One"), structOne(cell: 1, one: "One"), structOne(cell: 2, one: "One")]

arrayTwo = [structTwo(cell: 0, two: "Two"), structTwo(cell: 1, two: "Two"), structTwo(cell: 2, two: "Two")]

self.tableOne.delegate = self
self.tableOne.dataSource = self

self.tableTwo.delegate = self
self.tableTwo.dataSource = self

self.tableTwo.isHidden = true
self.tableOne.isHidden = false

self.tableTwo.reloadData()
self.tableOne.reloadData()

self.tableTwo.tableFooterView = UIView()
self.tableOne.tableFooterView = UIView()


// Do any additional setup after loading the view.
}

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

@IBAction func segmentAction(_ sender: Any) {
switch self.segmentDemo.selectedSegmentIndex
{
case 0:
self.tableTwo.isHidden = true
self.tableOne.isHidden = false
self.tableOne.reloadData()
case 1:
self.tableTwo.isHidden = false
self.tableOne.isHidden = true
self.tableTwo.reloadData()
default:
break
}
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableOne.isHidden == false{
return arrayOne.count
}
else {
return arrayTwo.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableOne.isHidden == false{
let cell = tableView.dequeueReusableCell(withIdentifier: "One") as! OneTableViewCell
cell.lblOne.text = arrayOne[indexPath.row].one
return cell
}
else {
let tcell = tableView.dequeueReusableCell(withIdentifier: "Two") as! TwoTableViewCell
tcell.labeltwo.text = arrayTwo[indexPath.row].two
return tcell
}
}

这是我的第一个单元格代码:

import UIKit

class OneTableViewCell: UITableViewCell {

@IBOutlet weak var lblOne: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

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

// Configure the view for the selected state
}

}

这是我的第二个单元格代码:

import UIKit

class TwoTableViewCell: UITableViewCell {

@IBOutlet weak var labeltwo: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

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

// Configure the view for the selected state
}

}

最佳答案

例如你有这两个数组。

var arrFirst    = ["One", "One", "One", "One", "One"]
var arrSecond = ["Second", "Second", "Second", "Second", "Second"]

全局定义

var nSelectedSegmentIndex : Int = 1 // 1 is for 1st SelgmentItem

UITableView代码

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if nSelectedSegmentIndex == 1 {
return arrFirst.count
}
else {
return arrSecond.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TableViewCell = self.tblVW.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

if nSelectedSegmentIndex == 1 {
cell.textLabel?.text = arrFirst[indexPath.row]
}
else {
cell.textLabel?.text = arrSecond[indexPath.row]
}

return cell;
}
}

分段操作

@IBAction func segmetnValueChange(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
self.nSelectedSegmentIndex = 1
}
else {
self.nSelectedSegmentIndex = 2
}
self.tblVW.reloadData()
}

关于ios - 在不使用 XIB CELL 的情况下使用 swift 4.0 中的段在一个 View Controller 中获取两个 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50349996/

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