Я хочу иметь несколько ячеек, каждая из которых имеет метку. Но в качестве последней ячейки я хочу иметь вместо метки кнопку, которая должна вернуться к моему основному контроллеру представления.
Ярлыки работают, но я не могу сделать ячейку с запущенной кнопкой. Вот мой код для UITableViewController:
class StatsViewController: UITableViewController {
var vc = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
if let label = cell.viewWithTag(1000)as? UILabel{
if indexPath.row == 0 {
label.text = "Row 0"
}
else if indexPath.row == 1{
label.text = "Row 1"
}
else if indexPath.row == 2{
label.text = "Row 2"
}
else if indexPath.row == 3{
label.text = "Row 3"
}
return cell
}
if let button = cell2.viewWithTag(1001) as? UIButton {
if indexPath.row == 4{
return cell2
}
}
return UITableViewCell()
}
}
Я идентифицировал ячейку метки в раскадровке с помощью «ячейки» и ячейку кнопки с «ячейкой2».
Затем я пометил метку значением «1000», и кнопка в ячейке 2 имеет Tag = 1001
(WEITER - это Кнопка)
Отредактировал мой код благодаря @Sh_Khan и @ вот так. Теперь он показывает мне кнопку, но не надписи ..
class StatsViewController: UITableViewController {
var vc = ViewController()
override func viewDidLoad() {
super.viewDidLoad()
// tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// tableView.reloadData()
//tableView.delegate = self
}
let timestamp = DateFormatter.localizedString(from: NSDate() as Date, dateStyle: .medium, timeStyle: .short)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellId = (indexPath.row == 4) ? "cell2" : "cell"
if let label = cell.viewWithTag(1000)as? UILabel{
if indexPath.row == 0 {
label.text = "Row 0"
}
else if indexPath.row == 1{
label.text = "Row 1"
}
else if indexPath.row == 2{
label.text = "Row 2"
}
else if indexPath.row == 3{
label.text = "Row 3"
}
return tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
}
if let button = cell2.viewWithTag(1001) as? UIButton {
if indexPath.row == 4{
return tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
}
}
return UITableViewCell()
}
}