我想用 XMSegmentedControl(https://github.com/xaviermerino/XMSegmentedControl) 更改 tableviewcell 的数据, 我在
中使用 switchfunc tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
,但它总是打印出来的
fatal error: unexpectedly found nil while unwrapping an Optional value**,and say *segmentedControl1 is nil.
let tin = ["1","2","3","4"]
let how = ["one","two","three","four"]
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var segmentedControl1: XMSegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let segmentedControl3 = XMSegmentedControl(frame: CGRect(x: 0, y: 70, width: self.view.frame.width, height: 44), segmentTitle: ["Hello", "World", "Three"], selectedItemHighlightStyle: XMSelectedItemHighlightStyle.topEdge)
segmentedControl3.backgroundColor = UIColor(red: 22/255, green: 150/255, blue: 122/255, alpha: 1)
segmentedControl3.highlightColor = UIColor(red: 25/255, green: 180/255, blue: 145/255, alpha: 1)
segmentedControl3.tint = UIColor.white
segmentedControl3.highlightTint = UIColor.black
self.view.addSubview(segmentedControl3)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
switch(segmentedControl1.selectedSegment)
{
case 0 :
returnValue = tin.count
case 1 :
returnValue = how.count
default :
break
}
return returnValue
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
switch(segmentedControl1.selectedSegment)
{
case 0 :
cell.textLabel?.text = tin[indexPath.row]
case 1 :
cell.textLabel?.text = how[indexPath.row]
default :
break
}
return cell
}
谁能帮我解决这个问题?
谢谢!
用我的代码替换您的 viewDidLoad()
。
override func viewDidLoad() {
super.viewDidLoad()
segmentedControl1 = XMSegmentedControl(frame: CGRect(x: 0, y: 70, width: self.view.frame.width, height: 44), segmentTitle: ["Hello", "World", "Three"], selectedItemHighlightStyle: XMSelectedItemHighlightStyle.topEdge)
segmentedControl1.backgroundColor = UIColor(red: 22/255, green: 150/255, blue: 122/255, alpha: 1)
segmentedControl1.highlightColor = UIColor(red: 25/255, green: 180/255, blue: 145/255, alpha: 1)
segmentedControl1.tint = UIColor.white
segmentedControl1.highlightTint = UIColor.black
self.view.addSubview(segmentedControl1)
}
放入segmentControl的valuechenged方法
DispatchQueue.main.async{
self.ttblView.reloadData()
}
您应用了错误的 segmentControl 。你把值放在 segmentedControl3 中,所以 segmentedControl1 给出 nil 。
关于ios - 使用 XMSegmentedControl 更改了 TableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43292453/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |