import UIKit
import ObjectiveC
var SubRowObjectKey: String = "subRow"
extension IndexPath {
var subRow: Int {
get {
let subRowObj = objc_getAssociatedObject(self, &SubRowObjectKey)
if subRowObj != nil {
return (subRowObj as! Int)
}
return 0
}
set {
let subRowObj = (newValue as NSInteger)
objc_setAssociatedObject(self, &SubRowObjectKey, subRowObj, .OBJC_ASSOCIATION_RETAIN)
}
}
static func indexPathForSubRow(_ subRow: NSInteger, inRow row: NSInteger, inSection section: NSInteger) -> IndexPath {
var indexPath = IndexPath(row: row, section: section)
indexPath.subRow = (subRow as Int)
print(subRow)
print(indexPath.subRow)
return indexPath
}
}
let indexPath = IndexPath.indexPathForSubRow(5, inRow: 1, inSection: 2)
print(indexPath.subRow)
在静态函数indexPathForSubRow中,'subRow'的数量为5(附图中第30行)
但是将subRow赋值给indexPath.subRow后,'indexPath.subRow'的数量为0,而不是5(附图中第29和31行)
在Xcode版本8.2.1和Swift 3.0中进行了测试
任何帮助都将不胜感激。
IndexPath
已经支持任意数量的索引。row
和section
只是方便访问索引0和1处的值的属性。只需将您的subRow
存储在索引路径的索引2处即可。 - rmaddy