swift 3 tableView 和 2 tableCellsViews 行为怪异
swift 3 tableView with 2 tableCellsViews acting weird
我有一个 viewController,它有一个 tableView,tableView 有 2 个 tableViewCell。单元格 1 和单元格 2。
这是我的 IB 观点
这是我的代码
import UIKit
class userMenuViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var myArr = ["aaa","bbb","ccc"]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (myArr.count)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0;
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell2TableViewCell
switch (indexPath.row) {
case 0:
return (cell1)
case 1:
return (cell2)
case 2:
return (cell1)
default:
return (cell1)
}
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
print(indexPath.row)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addSlideMenuButton()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
当我 运行 模拟器时,我得到的结果与我要求的完全相反,您可以从下面的屏幕截图中看到
更奇怪的是,在我向 viewDidLoad 添加以下代码之前:
DispatchQueue.main.async {
self.tableView.reloadData()
}
当模拟器 运行 时,单元格顺序是正确的,但在单击时它们会从一个更改为另一个,并且无法单击第一个单元格 [0]
任何帮助将不胜感激
尝试改变
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell2TableViewCell
switch (indexPath.row) {
case 0:
return (cell1)
case 1:
return (cell2)
case 2:
return (cell1)
default:
return (cell1)
}
至
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell1TableViewCell
return cell
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
return cell
}
我有一个 viewController,它有一个 tableView,tableView 有 2 个 tableViewCell。单元格 1 和单元格 2。
这是我的 IB 观点
这是我的代码
import UIKit
class userMenuViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var myArr = ["aaa","bbb","ccc"]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (myArr.count)
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100.0;
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell2TableViewCell
switch (indexPath.row) {
case 0:
return (cell1)
case 1:
return (cell2)
case 2:
return (cell1)
default:
return (cell1)
}
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
print(indexPath.row)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
addSlideMenuButton()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
当我 运行 模拟器时,我得到的结果与我要求的完全相反,您可以从下面的屏幕截图中看到
更奇怪的是,在我向 viewDidLoad 添加以下代码之前:
DispatchQueue.main.async {
self.tableView.reloadData()
}
当模拟器 运行 时,单元格顺序是正确的,但在单击时它们会从一个更改为另一个,并且无法单击第一个单元格 [0]
任何帮助将不胜感激
尝试改变
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell2TableViewCell
switch (indexPath.row) {
case 0:
return (cell1)
case 1:
return (cell2)
case 2:
return (cell1)
default:
return (cell1)
}
至
switch (indexPath.row) {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! PrefCell1TableViewCell
return cell
case 2:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! PrefCell1TableViewCell
return cell
}