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' Count = 5 (行号:附图中的 30)
但是在将 subRow 分配给 indexPath.subRow 之后,'indexPath.subRow' count = 0 而不是 5(附加图像中的第 29 和 31 行)
在 Xcode 版本 8.2.1 和 Swift 3.0 中测试
任何帮助将不胜感激。