我已經創建了一個 tableView,但是當我點擊它時,我沒有得到任何結果。我想添加一個新功能來改進我的專案,但我無法添加我觀看的視頻和我研究的東西。
表視圖
import UIKit
class ViewController1: UIViewController {
@IBOutlet weak var FoodView: UITableView!
let dogfoods = ["pork", "banana", "chicken-leg"]
override func viewDidLoad() {
super.viewDidLoad()
FoodView.delegate = self
FoodView.dataSource = self
// not tapped no see
FoodView.allowsSelection = false
}
}
extension ViewController1: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dogfoods.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = FoodView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
let dogfood = dogfoods[indexPath.row]
cell.foodImageView.image = UIImage(named: dogfood)
cell.nameLabel.text = dogfood
return cell
}
}
自定義單元格
class CustomCell: UITableViewCell {
@IBOutlet weak var dogView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var foodImageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
當我點擊圖片中的一個單元格時,我想寫一張放大的圖片和描述,我該怎么做?我在網上搜索時,我應用了類似的,但我沒有得到任何結果。
uj5u.com熱心網友回復:
你有幾個選擇。您可以向自定義單元格添加一個或多個按鈕。您可以將點擊手勢識別器附加到單元格的內容視圖。
回應整個單元格上的點擊的最簡單方法可能是讓視圖控制器符合UITableViewDelegate協議并實作該tableView(_:didSelectRowAt:)方法。
當用戶選擇一個單元格時將呼叫該方法,您將使用被點擊的單元格的 indexPath 來確定哪個單元格被點擊并執行任何適當的操作。
uj5u.com熱心網友回復:
您可以使用 Swift 中的 Delegate 以簡單高效的方式做到這一點
//創建協議,就像我們在 Java 中使用的介面一樣
@objc protocol TableViewCellDelegate {
@objc func click(indexPath: IndexPath?)
}
// 將您的類 CustomCell 修改為:
@IBOutlet weak var dogView: UIView!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var foodImageView: UIImageView!
var delegate: TableViewCellDelegate?
var indexPath: IndexPath?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
foodImageView.isUserInteractionEnabled = true
foodImageView.addGestureRecognizer(tapGestureRecognizer)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
let tappedImage = tapGestureRecognizer.view as! UIImageView
self.delegate.click?(indexPath: self.indexPath)
}
// 將 ViewController1 中的 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 修改為
let cell = FoodView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
cell.delegate = self
cell.indexPath = indexPath
let dogfood = dogfoods[indexPath.row]
cell.foodImageView.image = UIImage(named: dogfood)
cell.nameLabel.text = dogfood
return cell
// 現在添加擴展添加 ViewController1 的末尾
extension ViewController1: TableViewCellDelegate {
func click(indexPath: IndexPath?) {
// open image for preview here
}
}
uj5u.com熱心網友回復:
你可以通過多種方式做到這一點
使用添加一個 UITableViewDelegate 方法,它是
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ //你的代碼...
}
每次單擊單元格時它都會呼叫。
- 如果你更喜歡觸發任何按鈕點擊而不是單元格點擊然后去委托(Izaan Saleem 已經解釋過),或者你可以使用 NotificationCenter,但對于這個任務我更喜歡 didSelect 或委托解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/534965.html
標籤:迅速合适的视图表格视图
