我在swift 3中遇到了我的表视图控制器的问题。我有一个非常简单的Cell配置为原型,其中Image位于单元格的左侧,文本标签占用了3/4的空间在图像朝向单元格的右侧之后。每当我加载视图时,一切看起来都很好。但是,当我要么转换到新视图然后从导航控制器上的后退按钮返回到该视图,或者如果我向上滚动表格以将单元格移出视图并向下返回单元格视图拧紧并向左移动文本标签,或者删除图像或将图像叠加在文本标签的顶部。我在这里看到过一个类似的问题,但答案是客观的C或者根本不起作用。我已经尝试将加载逻辑放在不同的方法中,例如viewWillAppear,viewDidLoad等。我尝试过self.tableView.reload。我还尝试手动处理约束并使用tableViews estimatedHeight方法。有人能帮忙吗?这是我的班级:
import UIKit
import Parse
var bikeList = [String]()
var customerBikeIDList = [String]()
var customerIndex: Int = 0;
class BikeListController: UITableViewController {
@IBAction func addBike(_ sender: Any) {
// let storyboard = UIStoryboard(name: "Main", bundle: nil)
//let vc = storyboard.instantiateViewController(withIdentifier: "addBikeVC")
//self.present(vc, animated: true, completion: nil)
self.performSegue(withIdentifier: "addBike", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
//associate user to this device for notification usage
CommonUtils.addFCMTokenToParse()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
//was func viewWillAppear
override func viewWillAppear(_ animated: Bool) {
let query = PFQuery(className: "Bike")
if let userID = PFUser.current()?.objectId {
print("user ID: "+userID)
query.whereKey("userID", equalTo: userID)
query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in
if error == nil {
if let objects = objects {
for object in objects {
print("printing items from parse query")
print(object["userID"])
print(object["model"])
if(!(bikeList.contains(object["model"] as! String))){
bikeList.append(object["model"] as! String)
customerBikeIDList.append(object.objectId!)
}
}
self.tableView.reloadData()
}
}else {
//there was an error
print("There was an error...")
}
})
}
}
//no reason for having this function here other then idk
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return (bikeList.count)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BikeListCell
cell.theText.text = bikeList[indexPath.row]
// Configure the cell...
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
customerIndex = indexPath.row
self.performSegue(withIdentifier: "customerViewBike", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addBike" {
let navVC = segue.destination as? UINavigationController
_ = navVC?.viewControllers.first as! AddBikeViewController
}
}
}
这是BikeListCell
import UIKit
class BikeListCell: UITableViewCell {
@IBOutlet var theText: UILabel!
@IBOutlet var bikePhoto: UIImageView!
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
}
}