代码之家  ›  专栏  ›  技术社区  ›  user9606399

在tableview中添加行时如何更改textfield中的textcolor

  •  0
  • user9606399  · 技术社区  · 7 年前

    我有两个按钮。一个是@addIncomeTapped,另一个是@addexpensetrapped。我想做的是,一旦用户点击(例如)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

    1 回复  |  直到 7 年前
        1
  •  0
  •   DionizB    7 年前
    1. 第一类名称以大写开头。
    2. 其次,关于您的代码,您需要添加一些代码:
    3. 在…上 secondViewController 您将添加另外两个声明, var budgetTitleTextFieldColor = UIColor.black 因为黑色是文本或您使用的任何颜色的默认颜色 var budgetAmountTextFieldColor = UIColor.black 与相同 budgetTitleTextFielColor
    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()
      }