如何解决我如何在 tableView
`你好! 我有一个 tableview 和 2 个数组。 当我在选项卡之间切换时,tableview 会重新加载。在第一个选项卡中,我单击收藏夹,更改颜色并将数据添加到另一个数组。切换到第二个选项卡时,颜色不会改变。我该如何实施?
我的细胞
protocol CellSubclassDelegate: class {
func gestureTapped(cell: StocksCell)
}
class StocksCell: UITableViewCell {
@IBOutlet weak var logoImageView: UIImageView!
@IBOutlet weak var tickerLabel: UILabel!
@IBOutlet weak var favouriteImageView: UIImageView!{ didSet {
let panGesture = UITapGestureRecognizer(target: self,action: #selector(tapToAddFavourite))
favouriteImageView.addGestureRecognizer(panGesture)
favouriteImageView.isUserInteractionEnabled = true
}
}
@IBOutlet weak var companyNameLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var deltaLabel: UILabel!
var selectedCell = false
weak var delegate: CellSubclassDelegate?
override func awakeFromNib() {
super.awakeFromNib()
}
@objc private func tapToAddFavourite(_ recognizer: UITapGestureRecognizer) {
guard recognizer.state == .ended else { return }
if selectedCell{
favouriteImageView.tintColor = UIColor.lightGray
selectedCell = false
}else{
favouriteImageView.tintColor = UIColor.yellow
selectedCell = true
}
self.delegate?.gestureTapped(cell: self)
}
}
我的控制器
class StocksViewController: UIViewController,CellSubclassDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var stocksLabel: UILabel! { didSet {
let tapGestureStocks = UITapGestureRecognizer(target: self,action: #selector(tapToStocks))
stocksLabel.addGestureRecognizer(tapGestureStocks)
stocksLabel.isUserInteractionEnabled = true
}
}
@IBOutlet weak var favouriteLabel: UILabel!{ didSet {
let tapGestureFavourite = UITapGestureRecognizer(target: self,action: #selector(tapToFavourite))
favouriteLabel.addGestureRecognizer(tapGestureFavourite)
favouriteLabel.isUserInteractionEnabled = true
}
}
fileprivate var stocksData = [StocksModel(n: "VNDX",f: "Vandex,LLC",t: "4 764,6 ₽",tt: "+55 ₽ (1,15%)"),StocksModel(n: "DDD",f: "Dandex,t: "1 764,tt: "+155 ₽ (1,15%)")]
var favouriteData = [StocksModel]()
let privateIdentifire = "StocksCell"
var isstocksSelected = true
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
@objc private func tapToFavourite(_ recognizer: UITapGestureRecognizer) {
guard recognizer.state == .ended else { return }
favouriteLabel.alpha = 1
favouriteLabel.font = favouriteLabel.font.withSize(28)
stocksLabel.alpha = 0.65
stocksLabel.font = stocksLabel.font.withSize(18)
stocksLabel.textAlignment = .center
isstocksSelected = false
tableView.reloadData()
}
@objc private func tapToStocks(_ recognizer: UITapGestureRecognizer) {
guard recognizer.state == .ended else { return }
favouriteLabel.alpha = 0.65
favouriteLabel.font = favouriteLabel.font.withSize(18)
stocksLabel.alpha = 1
stocksLabel.font = stocksLabel.font.withSize(28)
stocksLabel.textAlignment = .left
isstocksSelected = true
tableView.reloadData()
}
}
表格数据源
extension StocksViewController: UITableViewDataSource{
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
let result = isstocksSelected ? stocksData.count : favouriteData.count
return result
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: privateIdentifire,for: indexPath) as! StocksCell
cell.delegate = self
if isstocksSelected{
cell.tickerLabel.text = stocksData[indexPath.row].name
cell.companyNameLabel.text = stocksData[indexPath.row].fullname
cell.priceLabel.text = stocksData[indexPath.row].ticker
cell.deltaLabel.text = stocksData[indexPath.row].tq
}else{
cell.tickerLabel.text = favouriteData[indexPath.row].name
cell.companyNameLabel.text = favouriteData[indexPath.row].fullname
cell.priceLabel.text = favouriteData[indexPath.row].ticker
cell.deltaLabel.text = favouriteData[indexPath.row].tq
}
return cell
}
func gestureTapped(cell: StocksCell) {
guard let indexPath = self.tableView.indexPath(for: cell) else {return}
if cell.selectedCell{
let dataStock = stocksData[indexPath.row]
favouriteData.append(dataStock)
}else{
favouriteData.remove(at: indexPath.row)
}
}
}
TABLEVIEW 委托
extension StocksViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 68
}
func tableView(_ tableView: UITableView,willdisplay cell: UITableViewCell,forRowAt indexPath: IndexPath) {
cell.layer.cornerRadius = 16
}
}
解决方法
在此处更改颜色:
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: privateIdentifire,for: indexPath) as! StocksCell
cell.delegate = self
if isStocksSelected{
favouriteImageView.tintColor = UIColor.lightGray
cell.tickerLabel.text = stocksData[indexPath.row].name
cell.companyNameLabel.text = stocksData[indexPath.row].fullname
cell.priceLabel.text = stocksData[indexPath.row].ticker
cell.deltaLabel.text = stocksData[indexPath.row].tq
}else{
favouriteImageView.tintColor = UIColor.yellow
cell.tickerLabel.text = favouriteData[indexPath.row].name
cell.companyNameLabel.text = favouriteData[indexPath.row].fullname
cell.priceLabel.text = favouriteData[indexPath.row].ticker
cell.deltaLabel.text = favouriteData[indexPath.row].tq
}
return cell
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。