这是我的代码:
@IBOutlet weak var billSelectorBtn: UIButton!
@IBOutlet weak var optionListTB: UITableView!
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return optionsListNames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as UITableViewCell
cell.textLabel?.text = optionsListNames[indexPath.row] as? String
return cell
}
private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
let selectedItem = optionsListNames.object(at: indexPath.row) as! NSString
billSelectorBtn.setTitle(selectedItem as String, for: UIControlState.normal)
if selectedItem .isEqual(to: "Current Bill")
{
self.view.backgroundColor = UIColor.red
}
else if selectedItem .isEqual(to: "Water Bill")
{
self.view.backgroundColor = UIColor.green
}
optionListTB.isHidden = true
}
因此,在我的didselectrow method
中,我正在检查所选字符串是否相等。如果它相等,我将保留 UI 作为某些颜色变化。但它不起作用??
我看到了本教程:http://www.aegisiscblog.com/how-to-implement-custom-dropdown-list-in-swift.html
请帮帮我..
谢谢
tableview
默认颜色是您在情节提要中添加的白色。要tableview
颜色与其背景视图相匹配,您必须清除tableview
颜色,Uitableviewcell
反之亦然。
在您的viewdidload
中添加此行,
tableview.backgroundColor = UIColor.clear //tableview is your tableview outlet
而在cellForRowAt indexPath
加
cell.backgroundColor = UIColor.clear
在 swift 3 中,didselect
语法已更改为
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
而不是isEqual
,使用 ">==" 来比较字符串。并使您的tableView
的背景颜色和单元格的背景颜色清除以查看颜色变化,如果您的表格视图占据全屏。
从委托方法中删除private
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
/// add implementation
}
参见