在具有核心数据的表视图中按行设置应​​用程序徽章编号

发布于 2024-09-15 08:39:59 字数 501 浏览 1 评论 0原文

我正在开发一个使用核心数据来保存和检索数据的应用程序。

现在我想在主屏幕的应用程序图标上添加徽章编号,但我遇到了一些问题...
我尝试了这段代码:

NSInteger section = [self.tableView numberOfSections];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects];

这段代码不起作用,但我不知道该怎么做...
我必须编写什么代码以及在哪里?在 AppDelegate 或 RootViewController 中?以及用什么方法呢?

感谢大家的帮助!

I'm developing an app that uses Core Data for save and retrieve data.

Now I would like to add a badge number on app's icon in the home screen but I have some problems...
I tried this code:

NSInteger section = [self.tableView numberOfSections];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects];

This code don't work but I don't know how to do this...
What code I must write and where? Inside AppDelegate or RootViewController? And in which method?

Thanks all for the help!

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

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

发布评论

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

评论(2

醉城メ夜风 2024-09-22 08:39:59
NSInteger section = [self.tableView numberOfSections];

这不是有效的节号;它们从 0 到 numberOfSections-1。

NSInteger section = [self.tableView numberOfSections];

That's not a valid section number; they go from 0 to numberOfSections-1.

陈甜 2024-09-22 08:39:59
NSInteger section = [[self tableView] numberOfSections];

这将返回 1 或更多。不是要插入 [[[self fetchedResultsController]sections] objectAtIndex:section] 的值。您应该给它一个实际的部分(零个或多个)而不是部分的总数。

此外,您可以在 sectionInfo 变量的赋值上放置一个断点,并确保不会返回 nil。您还可以在sectionInfo 之后进行调试,以确保您得到有意义的信息。

如果为零或返回零,则您的徽章将不会显示。

NSInteger section = [[self tableView] numberOfSections];

This is going to return back 1 or more. Not a value to be plugging into [[[self fetchedResultsController] sections] objectAtIndex:section]. You should be giving it an actual section (zero or more) not the total number of sections.

In addition you can put a breakpoint on your assignment of the sectionInfo variable and make sure you are not getting a nil back. You can also put a debug after the sectionInfo to make sure you are getting something meaningful back.

If it is nil or returning zero then your badge will not show.

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