ios 如何将 int 转换为 nsindexpath?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29428090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert int to nsindexpath?
提问by Adam Young
I am making a basic quiz application.The choices are in the form of a table view. If the user moves to a previous question, I want to show him his previously selected choice. I have stored the choice in NSUserDefaults. How do I convert this int value to NSIndexpath and use it in my tableview?
我正在制作一个基本的测验应用程序。选择是表格视图的形式。如果用户移动到上一个问题,我想向他展示他之前选择的选项。我已将选择存储在 NSUserDefaults 中。如何将此 int 值转换为 NSIndexpath 并在我的 tableview 中使用它?
回答by zisoft
See the NSINdexPath UIKit Additions
In your case you can use (Swift):
在您的情况下,您可以使用(Swift):
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
Swift 3.0:
斯威夫特 3.0:
let indexPath = IndexPath(row: 0, section: 0)
回答by Gil Sand
Duplicate of This post
这篇文章的重复
I copied the recieved answer here
我在这里复制了收到的答案
For an int index:
对于 int 索引:
NSIndexPath *path = [NSIndexPath indexPathWithIndex:index];
Creates Index of the item in node 0 to point to as per the reference.
创建节点 0 中项目的索引以根据引用指向。
To use the indexPath in a UITableView, the more appropriate method is
要在 UITableView 中使用 indexPath,更合适的方法是
NSIndexPath *path = [NSIndexPath indexPathForRow:yourInt inSection:section];
回答by Aman_Neuron
You can get indexpath using below code
您可以使用以下代码获取索引路径
NSIndexPath *index = [NSIndexPath indexPathForRow:YOURINTSTORED inSection:0];