ios 如何从 int 创建 NSIndexPath?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12997481/
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 create an NSIndexPath from an int?
提问by Deepak R
Error message:
错误信息:
cast of 'int' to "NSIndexPath *' is disallowed with ARC
ARC 不允许将“int”转换为“NSIndexPath *”
Code:
代码:
NSInteger CurrentIndex;
[self tableView:(UITableView *)horizontalTableView1 didSelectRowAtIndexPath:(NSIndexPath *)int_CurrentIndex];
How do I fix this error?
我该如何解决这个错误?
回答by TheTiger
You can't cast an int
variable to an NSIndexPath
directly. But you can make an NSIndexPath
from an int
variable.
您不能直接将int
变量强制转换为 an NSIndexPath
。但是你可以NSIndexPath
从一个int
变量中创建一个。
You need a section
index and row
index to make an NSIndexPath
. If your UITableView
has only one section
then:
您需要一个section
索引和row
索引来制作NSIndexPath
. 如果你UITableView
只有一个,section
那么:
Objective-C
目标-C
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
Swift
迅速
let indexPath: NSIndexPath = NSIndexPath(forRow: rowIndex, inSection: 0)
Swift 4
斯威夫特 4
let indexPath = IndexPath(row: rowIndex, section: 0)
回答by Chandramani
Swift 3.0
Swift 3.0
let indexPath : IndexPath = IndexPath(item: currentIndex, section: 0)