【问题标题】:how to change textcolor in textfield when I add a row in tableview当我在表格视图中添加一行时如何更改文本字段中的文本颜色
【发布时间】:2018-04-13 04:19:37
【问题描述】:

我有两个按钮。一个是@addIncomeTapped,另一个是@addExpenseTapped。我想做的是在用户点击(例如)addIncomeTapped 按钮后更改我的文本字段中的文本颜色。我正在尝试弄清楚但没有运气:'(我希望有人可以帮助我?我仍在努力学习更多。提前谢谢。

import UIKit
class MySecondCell:UITableViewCell,UITextFieldDelegate
{    
    @IBOutlet weak var budgetTitle: UITextField!
    @IBOutlet weak var budgetAmount: UITextField!
}

class secondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

   var textfield: UITextField!    
   var SecondBudgetName = [String]()

   @IBOutlet weak var balanceView: UIView!
   @IBOutlet weak var mytableView: UITableView!

   override func viewDidLoad() {
    super.viewDidLoad()

    mytableView.delegate = self
    mytableView.dataSource = self

    view.addVerticalGradientLayer(topColor: primaryColor, bottomColor: secondaryColor)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func addIncomeTapped(_ sender: Any) {
print("Add income tapped")
    //Add new row
    insert()
}

@IBAction func addExpenseTapped(_ sender: Any) {
print("Add expense tapped")
    //Add new row
    insert()

}

func insert(){
    SecondBudgetName.append("\(SecondBudgetName.count + 1)")
    let insertionIndexPath = IndexPath(row: SecondBudgetName.count - 1 , section: 0)
    mytableView.insertRows(at: [insertionIndexPath], with: .automatic)
}

//Configure TableView

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete {
        SecondBudgetName.remove(at: indexPath.row)
        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        tableView.endUpdates()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return SecondBudgetName.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = mytableView.dequeueReusableCell(withIdentifier: "budgetCell") as! MySecondCell

    return cell

}

}

here is my tableviewcontroller

【问题讨论】:

    标签: swift4 xcode9 xcode9.2 swift4.1


    【解决方案1】:
    1. 第一类名称以大写开头。
    2. 其次,关于您的代码,您需要添加一些代码:
    3. secondViewController 上,您将添加另外两个声明, var budgetTitleTextFieldColor = UIColor.black 因为黑色是文本的默认颜色或您使用的任何颜色 var budgetAmountTextFieldColor = UIColor.blackbudgetTitleTextFielColor 相同
    4. 添加到

       func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      let cell = mytableView.dequeueReusableCell(withIdentifier: "budgetCell") as! MySecondCell
      cell.budgetTitle.textColor = budgetTitleTextFieldColor
      cell.budgetAmount.textColor = budgetAmountTextFieldColor
      return cell
      }
      
    5. 然后在按钮操作中

      @IBAction func addIncomeTapped(_ sender: Any) {
          print("Add income tapped")
          //Add new row
          insert()
          // here you change the colors we declared in the beginning, it depends which one you want to change
          budgetTitleTextFieldColor = UIColor.whateverYourColorIs
          budgetAmountTextFieldColor = UIColor.whateverYourColorIs
          // don't forget this
          tableView.reloadData()}
       @IBAction func addExpenseTapped(_ sender: Any) {
             print("Add expense tapped")
             //Add new row
             insert()
             // here you change the colors we declared in the beginning
             budgetTitleTextFieldColor = UIColor.whateverYourColorIs
             budgetAmountTextFieldColor = UIColor.whateverYourColorIs
             // don't forget this
            tableView.reloadData()
      }
      

    【讨论】:

    • thankyousomuch 的回复。我尝试了你所说的,但是一旦我添加了“tableView.reloadData()”,我就会收到这个错误“对成员'tableView(_:commit:forRowAt:) 的模糊引用”。我错过了一些代码吗?
    • 你的table view被命名为myTableView,所以你得把tableView.reloadData()改成myTableView.reloadData()
    • 很抱歉问了这么多问题,但是。为什么我添加的前几行也发生了变化。例如。我已经插入了 5 行。一旦我点击“添加费用按钮”,所有行中的文本颜色都会变成红色,与“添加收入按钮”相同,它都会变成绿色。 :( 也许我应该替换 reloadData。但我不知道该使用什么。
    • 由于您重新加载表格视图数据,每一行都将重新加载,并且每一行都将获得您设置的颜色。那么您要更改哪种行颜色?
    • 我想做的是。当我按下“添加收入按钮”时,它的文本颜色将保持绿色。当我按下“添加费用按钮”时,新插入的行文本颜色也将保持红色。
    猜你喜欢
    • 2012-08-17
    • 1970-01-01
    • 2017-09-23
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多