核心数据获取 int 未显示在表格视图单元格上
我正在编写快速代码,目的是在每个表格视图单元格上显示越来越多的数字。现在 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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是它不起作用的原因。你有这样的:
这是一种非常复杂的尝试方法,而且它并不像写的那样工作。问题是
position
的值是一个Int64
并且您需要一个字符串。但是像这样使用as?
不会将其转换为字符串。当这行代码运行时,斯威夫特说,我可以把它变成一个字符串吗?但它不能。那么as? String
为 nil,并且您的表格单元格不包含该数字,因为转换失败。更好的方法是这样的
,但只有当您出于某种原因确实想使用
value(forKey:)
时才可以。您可能不需要它,因为通常 Xcode 会为每个具有命名属性的实体创建NSManagedObject
的子类。所以更好的是这两者都可以工作,因为字符串插值知道如何将整数转换为字符串。
Here's why it doesn't work. You have this:
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 anInt64
and you need a string. But usingas?
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 theas? String
is nil, and your table cells don't include the number because the conversion failed.A better way would be something like
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 ofNSManagedObject
for each entity with named properties. So even better would beThese both work because string interpolation knows how to convert an integer to a string.
您可能应该在 context.save() 之后调用 .reloadData()
You probably should call .reloadData() after context.save()