【发布时间】:2017-05-26 10:52:09
【问题描述】:
import UIKit
import MapKit
import CoreLocation
class CourseClass2: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak
var map: MKMapView!
@IBOutlet weak
var mapCourse: UIImageView!
@IBOutlet weak
var tableView: UITableView!
struct User {
var name: String
var images: UIImage
var coordinate: (Double, Double)
var type: String
var address: String
}
var rows = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
insertRowsMode3()
}
func insertRowsMode2() {
for i in 0.. < users.count {
insertRowMode2(ind: i, usr: users[i])
}
}
func insertRowMode2(ind: Int, usr: User) {
let indPath = IndexPath(row: ind, section: 0)
rows = ind + 1
tableView.insertRows(at: [indPath], with: .right)
}
func insertRowsMode3() {
rows = 0
insertRowMode3(ind: 0)
}
func insertRowMode3(ind: Int) {
let indPath = IndexPath(row: ind, section: 0)
rows = ind + 1
tableView.insertRows(at: [indPath], with: .right)
guard ind < users.count - 1
else {
return
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
self.insertRowMode3(ind: ind + 1)
}
}
func numberOfSections( in tableView: UITableView) - > Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - > Int {
return rows
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath) as!MyTableViewCell
let user = users[indexPath.row]
cell.MyImage.image = user.images
cell.MyLabel.text = user.name
cell.MyTypeLabel.text = user.type
return (cell)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "goToLast", sender: users[indexPath.row])
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) - > CGFloat {
return 100
}
override func prepare(
for segue: UIStoryboardSegue, sender: Any ? ) {
if segue.identifier == "goToLast" {
guard
let vc = segue.destination as ? FinalClass
else {
return
}
let guest = segue.destination as!FinalClass
if let user = sender as ? User {
guest.local = user.name
guest.localImage = user.images
guest.localType = user.type
guest.localAddress = user.address
}
}
}
@IBAction func IndTapped(_ sender: Any) {
self.performSegue(withIdentifier: "goBack", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
大家好,这是我收到错误的 ViewController 的代码,我添加了一个带有特定动画的 tableView,但是当我更改视图并返回 dismiss(animated: true, completion: nil)应用程序崩溃时,因为存在一些不一致与行数据。如您所见,当我第一次访问此CourseClass2 控制器时,我通过在viewDidAppear 中调用insertRowsMode3 来设置行。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
insertRowsMode3()
}
问题是当我回来时,行数据与我用零初始化的不同,并且 viewDidAppear 没有被调用。 我知道错误,但我不知道如何更改代码以使其正常工作。我真的需要帮助。
应用完全在这个函数中迷恋
func insertRowMode3(ind:Int) {
let indPath = IndexPath(row: ind, section: 0)
rows = ind + 1
tableView.insertRows(at: [indPath], with: .right)
guard ind < users.count-1 else { return }
DispatchQueue.main.asyncAfter(deadline: .now()+0.15) {
self.insertRowMode3(ind: ind+1)
}
}
这里tableView.insertRows(at: [indPath], with: .right)
【问题讨论】:
-
请对您发布的代码进行一些审查。它就像200行。只留下我们需要查看的代码。谢谢
-
放置断点并检查遇到此错误的位置。
-
编辑@RinkiNegi