Je crée une cellule personnalisée à partir d'un XIB
fichier et je l'implémente dans mon tableView
, je peux implémenter le texte de la cellule personnalisée, mais comment puis-je implémenter la même chose pour le bouton et recevoir des touches dessus ?
Voici le code que j'ai écrit :
struct cellData {
let cell : Int!
let text : String!
let button = UIButton()
}
var arrayOfData = [cellData]()
override func viewDidLoad() {
super.viewDidLoad()
arrayOfData = [cellData(cell : 1, text: "ahfhasdf", button: button)]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if arrayOfData[indexPath.row].cell==1{
let cell=Bundle.main.loadNibNamed("CustomTableViewCell1", owner: self, options: nil)?.first as! CustomTableViewCell1
cell.label1.text=arrayOfData[indexPath.row].text
cell.pillImage.currentImage=arrayOfData[indexPath].pillImage
return cell
}
Mon pill
est le bouton dans CustomTableViewCell1
lequel je veux dans ma cellule et comment avoir des actions lorsque le bouton est enfoncé ? S'il vous plaît aider.
C'est loin de ce que vous demandez dans votre question, mais je pense que vous cherchez quelque chose comme ça. J'espère que le code et les commentaires parlent d'eux-mêmes.
// My cell from xib (storyboard)
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
@IBOutlet weak var cellImageView: UIImageView!
@IBOutlet weak var label: UILabel!
}
// My view controlle from xib (storyboard)
class MyViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
fileprivate var dataSource: [CellData] = [CellData]()
override func viewDidLoad() {
super.viewDidLoad()
dataSource = [CellData(index: 0, title: "First cell", image: nil, target: self, selector: #selector(firstCellPressed)),
CellData(index: 1, title: "Another cell", image: nil, target: self, selector: #selector(anyOtherCellPressed))]
tableView.reloadData()
}
@objc private func firstCellPressed() {
print("First cell pressed")
}
@objc private func anyOtherCellPressed(sender: UIButton) {
print("Cell at row \(sender.tag) pressed")
}
}
fileprivate extension MyViewController {
struct CellData {
let index: Int
let title: String?
let image: UIImage?
let target: Any?
let selector: Selector
}
}
extension MyViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier", for: indexPath) as! MyTableViewCell // Dequeue cell
let source = dataSource[indexPath.row] // Get the source object
cell.button.tag = indexPath.row // Assign tag so we can use it in the button action method
// Assign target and selector if it exists
if let target = source.target {
cell.button.addTarget(target, action: source.selector, for: .touchUpInside)
}
cell.cellImageView.image = source.image // Assign image
cell.label.text = source.title // Assign title
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}
}
C'est quand même loin de ce que je ferais mais n'hésitez pas à expérimenter.
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots