|
@@ -81,7 +81,18 @@ class KMPageRangeTools: NSObject {
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
- class func parseSelectedIndexs(selectedIndex: Array<Int>) -> String {
|
|
|
+ private class func parseSelectedIndexs(selectedIndex: Array<Int>) -> String {
|
|
|
+ if (selectedIndex.count <= 0) {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ if (selectedIndex.count == 1) {
|
|
|
+ return "\((selectedIndex.first)!)"
|
|
|
+ }
|
|
|
+ if (selectedIndex.count == 2) {
|
|
|
+ return "\((selectedIndex.first)!),\((selectedIndex.last)!)"
|
|
|
+ }
|
|
|
+
|
|
|
+ // 元素大于2个
|
|
|
var string: String = ""
|
|
|
var index = 0
|
|
|
var prePage = 0
|
|
@@ -89,79 +100,101 @@ class KMPageRangeTools: NSObject {
|
|
|
var cubeStart = NSNotFound
|
|
|
var cubeEnd = NSNotFound
|
|
|
for i in selectedIndex {
|
|
|
- if selectedIndex.count == 1 {
|
|
|
- string.append(String(i))
|
|
|
- } else if selectedIndex.count == 2 {
|
|
|
+ if index == 0 {
|
|
|
string.append(String(i))
|
|
|
- if index != selectedIndex.count-1 {
|
|
|
- string.append(",")
|
|
|
- }
|
|
|
} else {
|
|
|
- if index == 0 {
|
|
|
- string.append(String(i))
|
|
|
+ if i - prePage == 1 {
|
|
|
+ if count == 0 {
|
|
|
+ cubeStart = prePage
|
|
|
+ }
|
|
|
+
|
|
|
+ count += 1
|
|
|
} else {
|
|
|
- if i - prePage == 1 {
|
|
|
- if count == 0 {
|
|
|
- cubeStart = prePage
|
|
|
+ count = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ // 最后一个元素
|
|
|
+ if (index == selectedIndex.count-1) {
|
|
|
+ if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
|
|
|
+ if cubeStart == selectedIndex.first {
|
|
|
+ string.removeFirst()
|
|
|
+ } else {
|
|
|
+ string.append(",")
|
|
|
}
|
|
|
|
|
|
- count += 1
|
|
|
- } else {
|
|
|
- count = 0
|
|
|
- }
|
|
|
-
|
|
|
- if count == 0 || index == selectedIndex.count-1 { /// 块结束
|
|
|
- if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
|
|
|
- if cubeStart == selectedIndex.first {
|
|
|
- string.removeFirst()
|
|
|
- } else {
|
|
|
+ if index == selectedIndex.count-1 {
|
|
|
+ if count == 0 {
|
|
|
+ string.append(String(cubeStart))
|
|
|
+ string.append("-")
|
|
|
+ string.append(String(cubeEnd))
|
|
|
+
|
|
|
string.append(",")
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if index == selectedIndex.count-1 {
|
|
|
- if count == 0 {
|
|
|
- string.append(String(cubeStart))
|
|
|
- string.append("-")
|
|
|
- string.append(String(cubeEnd))
|
|
|
-
|
|
|
- string.append(",")
|
|
|
- string.append(String(i))
|
|
|
- } else {
|
|
|
- string.append(String(cubeStart))
|
|
|
- string.append("-")
|
|
|
- string.append(String(i))
|
|
|
- }
|
|
|
+ string.append(String(i))
|
|
|
} else {
|
|
|
string.append(String(cubeStart))
|
|
|
string.append("-")
|
|
|
- string.append(String(cubeEnd))
|
|
|
+ string.append(String(i))
|
|
|
}
|
|
|
} else {
|
|
|
- if prePage == selectedIndex.first {
|
|
|
- // string.append(",")
|
|
|
- } else {
|
|
|
- string.append(",")
|
|
|
- string.append(String(prePage))
|
|
|
- }
|
|
|
+ string.append(String(cubeStart))
|
|
|
+ string.append("-")
|
|
|
+ string.append(String(cubeEnd))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if prePage == selectedIndex.first {
|
|
|
|
|
|
- cubeStart = NSNotFound
|
|
|
- cubeEnd = NSNotFound
|
|
|
+ } else {
|
|
|
+ string.append(",")
|
|
|
+ string.append(String(prePage))
|
|
|
}
|
|
|
+
|
|
|
+ cubeStart = NSNotFound
|
|
|
+ cubeEnd = NSNotFound
|
|
|
}
|
|
|
- // else if count == 1 {
|
|
|
- // if index == selectedPages.count-1 {
|
|
|
- // if prePage == selectedPages.first {
|
|
|
- //// string.append(",")
|
|
|
- // } else {
|
|
|
- // string.append(",")
|
|
|
- // string.append(String(prePage))
|
|
|
- // }
|
|
|
- // }
|
|
|
- // }
|
|
|
- else if count >= 2 { /// 可成块
|
|
|
- cubeEnd = i
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ // 不是最后一个元素
|
|
|
+ if count == 0 { /// 块结束
|
|
|
+ if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
|
|
|
+ if cubeStart == selectedIndex.first {
|
|
|
+ string.removeFirst()
|
|
|
+ } else {
|
|
|
+ string.append(",")
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if index == selectedIndex.count-1 {
|
|
|
+ if count == 0 {
|
|
|
+ string.append(String(cubeStart))
|
|
|
+ string.append("-")
|
|
|
+ string.append(String(cubeEnd))
|
|
|
+
|
|
|
+ string.append(",")
|
|
|
+ string.append(String(i))
|
|
|
+ } else {
|
|
|
+ string.append(String(cubeStart))
|
|
|
+ string.append("-")
|
|
|
+ string.append(String(i))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ string.append(String(cubeStart))
|
|
|
+ string.append("-")
|
|
|
+ string.append(String(cubeEnd))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if prePage == selectedIndex.first {
|
|
|
+ // string.append(",")
|
|
|
+ } else {
|
|
|
+ string.append(",")
|
|
|
+ string.append(String(prePage))
|
|
|
+ }
|
|
|
+
|
|
|
+ cubeStart = NSNotFound
|
|
|
+ cubeEnd = NSNotFound
|
|
|
}
|
|
|
+ } else if count >= 2 { /// 可成块
|
|
|
+ cubeEnd = i
|
|
|
}
|
|
|
}
|
|
|
|