为什么初始分段索引 0 表视图值未加载到 Swift?
Why Initially segmented index 0 tableview values are not loading in Swift?
我正在使用 HMSegmentedControl
。当我 运行 时,tableview 显示为空,当我点击 index 1
时,tableview 显示.. 现在当我去 index 0
时,tableview 值正在显示。为什么最初我没有在 index 0
.
中获取 tableview 数据
代码如下:
import UIKit
import HMSegmentedControl
class PlansPrepaidViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let segmentedControl = HMSegmentedControl(sectionTitles: ["POPULAR", "SPECIAL RECHARGE", "TOP UP", "2G/3G/4G DATA", "FULL TALKTIME", "ROAMING"])
var sortedArray = [Any]()
let talktimeArray = ["26.66", "500.00","26.66", "500.00","26.66", "500.00","26.66", "500.00"]
let validityArray = ["42 Days", "28 Days", "52 Days", "18 Days", "76 Days", "23 Days", "42 Days", "120 Days"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
designSegmentedControl()
}
//for segment
func designSegmentedControl(){
segmentedControl?.frame = CGRect(x: 10, y: 60, width: 500, height: 60)
segmentedControl?.backgroundColor = UIColor(red: 4/255, green: 118/255, blue: 208/255, alpha: 1.0)
segmentedControl?.selectionIndicatorLocation = .down
segmentedControl?.selectionIndicatorHeight = 2.5
segmentedControl?.indexChangeBlock = { index in
print(index)
if index == 0{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
print("segmnent 0 \(self.sortedArray)")
}
if index == 1{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedDescending }
print("segmnent 1 \(self.sortedArray)")
}
if index == 2{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedSame }
print("segmnent 1 \(self.sortedArray)")
self.tableView.reloadData()
}
if index == 3{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
print("segmnent 1 \(self.sortedArray)")
self.tableView.reloadData()
}
}
self.view.addSubview(segmentedControl!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlansTableViewCell
cell.layoutIfNeeded()
cell.containerView.dropShadow()
cell.talktimeLabel.text = talktimeArray[indexPath.row]
cell.validityLabel.text = sortedArray[indexPath.row] as? String
return cell
}
}
首先为什么sortedArray
声明为[Any]
?类型明明是
var sortedArray = [String]()
table 视图在启动时不显示任何内容,因为 sortedArray
未填充。
要么插入
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
tableView.reloadData()
在 viewDidLoad
的末尾或插入一行强制调用 indexChangeBlock
处理程序。
我正在使用 HMSegmentedControl
。当我 运行 时,tableview 显示为空,当我点击 index 1
时,tableview 显示.. 现在当我去 index 0
时,tableview 值正在显示。为什么最初我没有在 index 0
.
代码如下:
import UIKit
import HMSegmentedControl
class PlansPrepaidViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let segmentedControl = HMSegmentedControl(sectionTitles: ["POPULAR", "SPECIAL RECHARGE", "TOP UP", "2G/3G/4G DATA", "FULL TALKTIME", "ROAMING"])
var sortedArray = [Any]()
let talktimeArray = ["26.66", "500.00","26.66", "500.00","26.66", "500.00","26.66", "500.00"]
let validityArray = ["42 Days", "28 Days", "52 Days", "18 Days", "76 Days", "23 Days", "42 Days", "120 Days"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
designSegmentedControl()
}
//for segment
func designSegmentedControl(){
segmentedControl?.frame = CGRect(x: 10, y: 60, width: 500, height: 60)
segmentedControl?.backgroundColor = UIColor(red: 4/255, green: 118/255, blue: 208/255, alpha: 1.0)
segmentedControl?.selectionIndicatorLocation = .down
segmentedControl?.selectionIndicatorHeight = 2.5
segmentedControl?.indexChangeBlock = { index in
print(index)
if index == 0{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
print("segmnent 0 \(self.sortedArray)")
}
if index == 1{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedDescending }
print("segmnent 1 \(self.sortedArray)")
}
if index == 2{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedSame }
print("segmnent 1 \(self.sortedArray)")
self.tableView.reloadData()
}
if index == 3{
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
print("segmnent 1 \(self.sortedArray)")
self.tableView.reloadData()
}
}
self.view.addSubview(segmentedControl!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PlansTableViewCell
cell.layoutIfNeeded()
cell.containerView.dropShadow()
cell.talktimeLabel.text = talktimeArray[indexPath.row]
cell.validityLabel.text = sortedArray[indexPath.row] as? String
return cell
}
}
首先为什么sortedArray
声明为[Any]
?类型明明是
var sortedArray = [String]()
table 视图在启动时不显示任何内容,因为 sortedArray
未填充。
要么插入
self.sortedArray = self.validityArray.sorted { [=11=].localizedCaseInsensitiveCompare() == ComparisonResult.orderedAscending }
tableView.reloadData()
在 viewDidLoad
的末尾或插入一行强制调用 indexChangeBlock
处理程序。