Обновление метки в ячейке - PullRequest
0 голосов
/ 12 июня 2018

У меня есть TableView, строки которого содержат метку и две кнопки.Что я хочу сделать, так это то, что когда пользователь нажимает первую кнопку «Установить имя», появляется всплывающее окно, в котором он может вводить текст с клавиатуры.После нажатия кнопки «Установить» всплывающее окно закрывается, и метка внутри строки, содержащей нажатую кнопку, изменяется на вводимый текст.Я установил делегатов, но не могу сделать метку для изменения.

TableView:

import UIKit

class SetGame: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    var numOfPlayers = Int()

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.Name.text = "Player \(indexPath.row + 1)"
        cell.btn1.tag = indexPath.row
        cell.btn2.tag = indexPath.row

        return cell
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    @IBAction func setName(sender: UIButton)
    {   
        let thisVC = storyboard?.instantiateViewController(withIdentifier: "SetName") as! SetName
        thisVC.delegate = self
        present(thisVC, animated: true, completion: nil)
    }

    @IBAction func setFingerprint(_ sender: UIButton)
    {

    }

    @IBAction func unwindToSetGame(_ segue: UIStoryboardSegue)
    {
        print("unwinded to SetGame")
    }

    @IBOutlet weak var tableView: UITableView!
}

extension SetGame: nameDelegate
{
    func named(name: String)
    {
        let indexP = IndexPath(row: 0, section: 0)
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell
        cell.Name.text = "bkjhvghcjhkv"
        //wanted to see if it changes first cell. But doesn't work

    }
}

TableViewCell Класс:

import UIKit

class TableViewCell: UITableViewCell
{

    override func awakeFromNib()
    {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    @IBOutlet weak var Name: UILabel!
    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!

}

Всплывающее представление:

import UIKit

protocol nameDelegate
{
    func named(name: String)
}

class SetName: UIViewController
{
    var delegate: nameDelegate!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        window.layer.borderWidth = 1
        window.layer.borderColor = UIColor.white.cgColor
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    @IBAction func closePopUp(_ sender: Any)
    {
        if input.text != ""
        {
            delegate.named(name: input.text!)
        }
        dismiss(animated: true, completion: nil)
    }

    @IBOutlet weak var input: UITextField!
    @IBOutlet weak var window: UIView!
}

1 Ответ

0 голосов
/ 12 июня 2018

Замените это

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell

на

let cell = tableView.cellForRow(at:indexP) as! TableViewCell
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...