核心数据获取 int 未显示在表格视图单元格上

发布于 2025-01-16 10:48:34 字数 1801 浏览 3 评论 0原文

我正在编写快速代码,目的是在每个表格视图单元格上显示越来越多的数字。现在 int 没有被显示。因此,第一个表格视图单元格应该显示 1,第二个表格视图单元格应该显示 2。您可以在下面的 gif 中看到表格视图单元格发生的情况,并且单击按钮时其中不会出现任何内容。下面的功能是单击按钮时的功能。

   var pageNumber = 1


 var itemName : [Player] = []

func enterData() {
    theScores.reloadData()
 
 let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
 
 let context = appDeldeaget.persistentContainer.viewContext
 
 // Simpler way to create a new Core Data object
 let theTitle = Player(context: context)

 // Simpler way to set the position attribute

    
    theTitle.positon = Int64(pageNumber)
    print(pageNumber)
 
    
    // pageNumber must be of type Int64, otherwise use Int64(pageNumber)
 
 do {
     try context.save()
     itemName.append(theTitle)
     pageNumber += 1
 } catch {
     // handle errors
 }
 
 

 }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let title = itemName[indexPath.row]
    let cell = theScores.dequeueReusableCell(withIdentifier: "MyCell", for : indexPath)
   
    cell.selectionStyle = .default

    let attr5 = title.value(forKey: "positon") as? String
    
    let text = [" Item :",   attr5].compactMap { $0 }.reduce("", +)
    cell.textLabel?.text = "\(text)"
    
    cell.textLabel?.textAlignment = .center
    
    cell.layoutMargins = UIEdgeInsets.zero

    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero
    
    
    return cell
    
}

输入图片此处描述输入图片此处描述

I am writing swift code with the goal of displaying a increasing number on every tableview cell. Right now the int is not being display. So the first tableview cell should say 1 and the 2nd should say 2. You can see in the gif below what is going along with the tableview cell and nothing is appearing in them when the button is clicked. The func below is when the button is clicked.

   var pageNumber = 1


 var itemName : [Player] = []

func enterData() {
    theScores.reloadData()
 
 let appDeldeaget = UIApplication.shared.delegate as! AppDelegate
 
 let context = appDeldeaget.persistentContainer.viewContext
 
 // Simpler way to create a new Core Data object
 let theTitle = Player(context: context)

 // Simpler way to set the position attribute

    
    theTitle.positon = Int64(pageNumber)
    print(pageNumber)
 
    
    // pageNumber must be of type Int64, otherwise use Int64(pageNumber)
 
 do {
     try context.save()
     itemName.append(theTitle)
     pageNumber += 1
 } catch {
     // handle errors
 }
 
 

 }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let title = itemName[indexPath.row]
    let cell = theScores.dequeueReusableCell(withIdentifier: "MyCell", for : indexPath)
   
    cell.selectionStyle = .default

    let attr5 = title.value(forKey: "positon") as? String
    
    let text = [" Item :",   attr5].compactMap { $0 }.reduce("", +)
    cell.textLabel?.text = "\(text)"
    
    cell.textLabel?.textAlignment = .center
    
    cell.layoutMargins = UIEdgeInsets.zero

    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero
    
    
    return cell
    
}

enter image description here
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以往的大感动 2025-01-23 10:48:34

这就是它不起作用的原因。你有这样的:

let attr5 = title.value(forKey: "positon") as? String

let text = [" Item :",   attr5].compactMap { $0 }.reduce("", +)

这是一种非常复杂的尝试方法,而且它并不像写的那样工作。问题是 position 的值是一个 Int64 并且您需要一个字符串。但是像这样使用 as? 不会将其转换为字符串。当这行代码运行时,斯威夫特说,我可以把它变成一个字符串吗?但它不能。那么 as? String 为 nil,并且您的表格单元格不包含该数字,因为转换失败。

更好的方法是这样的

if let position = title.value(forKey: "positon") {
    cell.textLabel?.text = "Item : \(positon))"
}

,但只有当您出于某种原因确实想使用 value(forKey:) 时才可以。您可能不需要它,因为通常 Xcode 会为每个具有命名属性的实体创建 NSManagedObject 的子类。所以更好的是

cell.textLabel?.text = "Item: \(title.position)"

这两者都可以工作,因为字符串插值知道如何将整数转换为字符串。

Here's why it doesn't work. You have this:

let attr5 = title.value(forKey: "positon") as? String

let text = [" Item :",   attr5].compactMap { $0 }.reduce("", +)

This is a really complicated way to try and do this, and it doesn't work as written. The problem is that the value of position is an Int64 and you need a string. But using as? like that doesn't turn it into a string. When that line of code runs, Swift says, can I just make this into a string? But it can't. So the as? String is nil, and your table cells don't include the number because the conversion failed.

A better way would be something like

if let position = title.value(forKey: "positon") {
    cell.textLabel?.text = "Item : \(positon))"
}

But that's only if you really want to use value(forKey:) for some reason. You probably don't need that because normally Xcode creates a subclass of NSManagedObject for each entity with named properties. So even better would be

cell.textLabel?.text = "Item: \(title.position)"

These both work because string interpolation knows how to convert an integer to a string.

心作怪 2025-01-23 10:48:34

您可能应该在 context.save() 之后调用 .reloadData()

You probably should call .reloadData() after context.save()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文