将数组数组转换为 swift 中的段
Convert Array of arrays to segments in swift
我有数组 [["PT"], ["GE", "DE", "PL", "BY"], ["CZ", "US"]]
,我想在我以编程方式创建的 UISegmentedControl
中使用它:
for i in 0..<array.count {
mySegmentControl.insertSegment(withTitle: array[i], at: i, animated: false)
}
我看到错误:
Cannot convert value of type '[String]' to expected argument type 'String?'
没错,但我需要 PT
出现在第一个片段标题中,GE..BY
出现在第二个片段标题中,依此类推。
数组的类型是什么?是 [[String]],那么你可以这样做(Playground 代码):
extension UISegmentedControl {
func updateTitle(array titles: [[String]]) {
removeAllSegments()
for t in titles {
let title = t.joined(separator: ", ")
insertSegment(withTitle: title, at: numberOfSegments, animated: true)
}
}
}
let control = UISegmentedControl()
control.updateTitle(array: [["PT"], ["GE", "DE", "PL", "BY"], ["CZ", "US"]])
control.titleForSegment(at: 1)
如果你想要 PT
在第一段,GE..BY
在第二段等等。所以这样试试。
for (index,subArray) in array.enumerated() {
if subArray.count > 1 {
let title = subArray.first! + ".." + subArray.last!
mySegmentControl.insertSegment(withTitle: title, at: index, animated: false)
}
else if subArray.count > 0 {
let title = subArray.first!
mySegmentControl.insertSegment(withTitle: title, at: index, animated: false)
}
}
另一种方法是将数组映射到标题,如下所示:
let titles: [String] = array.flatMap {
guard let first = [=10=].first else { return nil }
return first + ([=10=].count > 1 ? (".." + [=10=].last!) : "")
}
对于 let array = [["PT"], ["GE", "DE", "PL", "BY"], [], ["CZ", "US"]]
会产生 ["PT", "GE..BY", "CZ..US"]
.
然后将其插入您的 UISegmentedControl
:
titles.enumerated().forEach {
mySegmentControl.insertSegment(withTitle: [=11=].element, at: [=11=].offset, animated: false)
}
我有数组 [["PT"], ["GE", "DE", "PL", "BY"], ["CZ", "US"]]
,我想在我以编程方式创建的 UISegmentedControl
中使用它:
for i in 0..<array.count {
mySegmentControl.insertSegment(withTitle: array[i], at: i, animated: false)
}
我看到错误:
Cannot convert value of type '[String]' to expected argument type 'String?'
没错,但我需要 PT
出现在第一个片段标题中,GE..BY
出现在第二个片段标题中,依此类推。
数组的类型是什么?是 [[String]],那么你可以这样做(Playground 代码):
extension UISegmentedControl {
func updateTitle(array titles: [[String]]) {
removeAllSegments()
for t in titles {
let title = t.joined(separator: ", ")
insertSegment(withTitle: title, at: numberOfSegments, animated: true)
}
}
}
let control = UISegmentedControl()
control.updateTitle(array: [["PT"], ["GE", "DE", "PL", "BY"], ["CZ", "US"]])
control.titleForSegment(at: 1)
如果你想要 PT
在第一段,GE..BY
在第二段等等。所以这样试试。
for (index,subArray) in array.enumerated() {
if subArray.count > 1 {
let title = subArray.first! + ".." + subArray.last!
mySegmentControl.insertSegment(withTitle: title, at: index, animated: false)
}
else if subArray.count > 0 {
let title = subArray.first!
mySegmentControl.insertSegment(withTitle: title, at: index, animated: false)
}
}
另一种方法是将数组映射到标题,如下所示:
let titles: [String] = array.flatMap {
guard let first = [=10=].first else { return nil }
return first + ([=10=].count > 1 ? (".." + [=10=].last!) : "")
}
对于 let array = [["PT"], ["GE", "DE", "PL", "BY"], [], ["CZ", "US"]]
会产生 ["PT", "GE..BY", "CZ..US"]
.
然后将其插入您的 UISegmentedControl
:
titles.enumerated().forEach {
mySegmentControl.insertSegment(withTitle: [=11=].element, at: [=11=].offset, animated: false)
}