选择一个表格视图单元格时创建一个自定义视图

问题描述 投票:0回答:1

我将如何创建一个显示用于选择特定tableview单元格的唯一信息的新视图?例如,如果选择了第1行,则新图像将具有该单元格的唯一信息,该信息与选择第2行时的视图不同。

这是我的tableview的代码。

import UIKit

struct Data {
var sectionTitle = String()
var rowTitles = [String]()
}

class ViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var tableView: UITableView!

var dataArray = [Data(sectionTitle: "section 1", rowTitles: ["row 1", "row 2", "row 3"]),
    Data(sectionTitle: "section 2", rowTitles: ["row 1"]),
    Data(sectionTitle: "section 3", rowTitles: ["row 1", "row 2"])
]
var searchArray = [Data]()
var searching = false

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
}

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if searching {
        return searchArray[section].rowTitles.count
    } else {
        return dataArray[section].rowTitles.count
    }
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell")
    cell?.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
    cell?.textLabel?.numberOfLines = 3
    if searching {
        cell?.textLabel?.text = self.searchArray[indexPath.section].rowTitles[indexPath.row]
    } else {
        cell?.textLabel?.text = self.dataArray[indexPath.section].rowTitles[indexPath.row]
    }
    return cell!
}
func numberOfSections(in tableView: UITableView) -> Int {
    if searching {
        return searchArray.count
    } else {
        return dataArray.count
    }
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return dataArray[section].sectionTitle
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let index = indexPath.row
    let data = dataArray[index]
    let destVC = SecondViewController(data: data)
    navigationController?.pushViewController(destVC, animated: true)
}

}

extension ViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    if searchBar.text == nil || searchBar.text == "" {
        searching = false
        view.endEditing(true)
        tableView.reloadData()
    } else {
        searching = true
        searchArray =   dataArray.filter({$0.sectionTitle.lowercased().contains(searchBar.text!.lowercased()) || $0.rowTitles.map{$0.lowercased()}.contains(searchBar.text!.lowercased())})
        tableView.reloadData()
    }
}
}

这是我的第二个视图控制器,它显示了我想更改的通用堆栈视图,以根据表视图中选择的单元格显示不同的图像和标签。

import UIKit

class SecondViewController: UIViewController {

var data: Data!
var dataView: DataView { return self.view as! DataView}

init(data: Data) {
    self.data = data
    super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
    super.viewDidLoad()

}

override func loadView() {
    self.view = DataView(frame: UIScreen.main.bounds)
}

}


class DataView: UIView {
override init(frame: CGRect) {
    super.init(frame: frame)
    self.backgroundColor = .white
    setupViews()
    setupConstraints()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func setupViews() {
    self.addSubview(stack)
    stack.distribution = .fillProportionally
    stack.spacing = 10
}

func setupConstraints() {
    stack.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    stack.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0).isActive = true
    stack.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0).isActive = true
    stack.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true

    dataImage.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
    dataImage.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor, constant: 60).isActive = true
    dataImage.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor, constant: -60).isActive = true
}

let dataNameLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 20, weight: .bold)
    label.textAlignment = .center
    label.numberOfLines = 2
    label.text = "Organization"
    return label
} ()

let dataDescriptionLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
    label.textAlignment = .center
    label.numberOfLines = 8
    label.text = "Description"
    return label
} ()

let dataImage: UIImageView = {
    let imageView = UIImageView()
    imageView.image = UIImage(named: "")?.withRenderingMode(.alwaysOriginal)
    imageView.backgroundColor = .gray
    imageView.layer.cornerRadius = 8
    imageView.contentMode = .scaleAspectFill
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.heightAnchor.constraint(equalToConstant: 260).isActive = true
    return imageView
} ()

let visitButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Visit", for: .normal)
    return button
} ()

lazy var stack: UIStackView = {
    let stack = UIStackView(arrangedSubviews: [dataImage, dataNameLabel, dataDescriptionLabel, visitButton])
    stack.translatesAutoresizingMaskIntoConstraints = false
    stack.axis = .vertical
    return stack
} ()

}

目的是让用户选择一个表格视图单元格,并使堆栈视图显示与该单元格相对应的特定信息。我将如何创建呢?

ios swift xcode uitableview uiview
1个回答
0
投票

我不确定当您选择TableViewController中的一项时,您是否正在询问如何将信息传递给新的视图控制器。

但是我正在解释您的问题,因为您知道如何传递信息,但是您希望根据选择的TableViewCell将不同的信息(可能是图像)传递给第二个视图控制器。

我这样做的方法是在SecondViewController中有一个包含图像的数组。在TableViewController中选择一个项目时,还将indexPath.row传递给SecondViewController。从那里,您可以在传递给它的indexPath.row处从imagesArray加载图像。您可以通过在SecondViewController中使用一个整数值来实现此目的,该值在您选择之前就已填充。

© www.soinside.com 2019 - 2024. All rights reserved.