ios swift确实选择了行来检查字符串是否正常工作

时间:2017-05-27 20:03:11

标签: ios swift tableview

这是我的代码:

href=”favicon.ico?v=1” 

所以在我的 @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 } 我检查所选字符串是否相等。如果它相等,我会将ui保持为某种颜色。但它不起作用??

我看到了这个教程:http://www.aegisiscblog.com/how-to-implement-custom-dropdown-list-in-swift.html

请帮帮我..

由于

3 个答案:

答案 0 :(得分:1)

您在故事板中添加的

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) {


    }

答案 1 :(得分:0)

使用“ == ”来比较字符串,而不是isEqual。如果你的tableView占据全屏,那么让你的tableView的backgroundColor和cell的backgroundColor清除以查看颜色变化。

答案 2 :(得分:0)

从您的委托方法中删除private

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    /// add implementation
} 

See also