gpt4 book ai didi

ios - uitableViewCell 不更新数据,因为它只显示空白

转载 作者:行者123 更新时间:2023-11-28 07:33:38 25 4
gpt4 key购买 nike

我正在尝试创建一个 TableView ,该 TableView 使用从用户接收输入的文本字段显示在另一个 View Controller 中生成的数据,然后我使用核心数据保存这些输入,然后我想将它们加载到 tableView 单元格中,除非它们不'完全加载到细胞上。

我真的卡住了,我不知道该尝试什么

这是第一个包含tableView的VC

    import UIKit
import CoreData

let appDelegate = UIApplication.shared.delegate as? AppDelegate

class FriendsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var friends : [Friend] = []


override func viewDidLoad() {
super.viewDidLoad()
tableView.reloadData()
tableView.delegate = self
tableView.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
if friends.count >= 1{
let endIndex = IndexPath(row: friends.count - 1, section: 0)
tableView.scrollToRow(at: endIndex, at: .bottom, animated: false)
}
}
@IBAction func addFriendsButtonWasPressed(_ sender: Any) {
performSegue(withIdentifier: "toCreateFriends", sender: self)
}


func fetchCoreDataObjects(){
self.fetch { (complete) in
if complete{
if friends.count >= 1 {
tableView.isHidden = false
}else {
tableView.isHidden = true
}
}
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "FC") as? FriendCell else{
return UITableViewCell()
}
let friend = friends[indexPath.row]
cell.configureCell(friend: friend)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .none
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (rowAction , indexPath) in
self.removeFriend(atIndexPat: indexPath)
self.fetchCoreDataObjects()
tableView.deleteRows(at: [indexPath], with: .automatic)

}

deleteAction.backgroundColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
return [deleteAction]
}
}
extension FriendsVC {

func fetch(completion : (_ complete : Bool)->()){
guard let managedContext = appDelegate?.persistentContainer.viewContext else{return}
let fetchRequest = NSFetchRequest<Friend>(entityName: "Friend")
do{
friends = try managedContext.fetch(fetchRequest)
completion(true)
} catch{
debugPrint("Couldn't fetch\(error.localizedDescription)")
completion(false)
}
}

func removeFriend(atIndexPat indexPath: IndexPath){
guard let managedContext = appDelegate?.persistentContainer.viewContext else {return}
managedContext.delete(friends[indexPath.row])
do {
try managedContext.save()
} catch{
debugPrint("Couldn't remove\(error.localizedDescription)")
}

}
}

-------------------------------------------- ----------------------这是用户应该创建新 friend 数据的另一个 VC

    import UIKit
import CoreData

class CreateFriendsVC: UIViewController, UITableViewDelegate {

@IBOutlet weak var nameLbl: UITextField!
@IBOutlet weak var ageLbl: UITextField!
@IBOutlet weak var genderLbl: UITextField!
@IBOutlet weak var createBtn: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
createBtn.bindToKeyboard()
let tap = UITapGestureRecognizer(target: self, action: #selector(CreateFriendsVC.handleTap))
let pan = UIPanGestureRecognizer(target: self, action: #selector(CreateFriendsVC.handleTap))
view.addGestureRecognizer(tap)
view.addGestureRecognizer(pan)

}
@objc func handleTap(){
view.endEditing(true)
}

@IBAction func createBtnPressed(_ sender: Any) {
if nameLbl.text != "" && ageLbl.text != "" && genderLbl.text != "" {
self.save { (complete) in
if complete{
dismiss(animated: true, completion: nil)

}
}
}

dismiss(animated: true, completion: nil)
}

func save(completion : (_ finished : Bool)->()){
guard let managedContext = appDelegate?.persistentContainer.viewContext else{return}
let friend = Friend(context: managedContext)
friend.age = ageLbl.text!
friend.gender = genderLbl.text!
friend.name = nameLbl.text!
do{
try managedContext.save()
completion(true)
print("Save success")
print(friend.age!)
} catch{
debugPrint("Couldn't Save: \(error.localizedDescription)")
completion(false)
}
}

}

============================================= =========================这是自定义单元生成器

    import UIKit

class FriendCell: UITableViewCell {
@IBOutlet weak var nameTxtLbl: UILabel!
@IBOutlet weak var ageTxtLbl: UILabel!
@IBOutlet weak var genderTxtLbl: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

func configureCell(friend : Friend){
self.nameTxtLbl.text = friend.name
self.ageTxtLbl.text = friend.age
self.genderTxtLbl.text = friend.gender

}

}

预期结果应该是一个表格 View ,其中的单元格显示所有用户输入的数据。实际结果是一个空的表格 View 单元格

最佳答案

重载前需要在viewWillAppear中执行fetchCoreDataObjects()

 override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
fetchCoreDataObjects()
self.tableView.reloadData()
if friends.count >= 1{
let endIndex = IndexPath(row: friends.count - 1, section: 0)
tableView.scrollToRow(at: endIndex, at: .bottom, animated: false)
}
}

关于ios - uitableViewCell 不更新数据,因为它只显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53907046/

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