2016-09-27 40 views
2

Ich habe eine Tabellenansicht, wo ich ein Label und zwei Schaltflächen erstellt habe. Ich stehe fest, während ich den Text vom Label auf Knopfdruck bekomme. Ich habe eine Array-Liste wie geschaffen:Tabellenansicht Zellentext in Tabellenansicht Zelle Schaltfläche

let arrayList: [String] = [ "aaa" , "bbb" , "ccc"] 

Ich möchte, wenn ich den Knopf auf index[0] klicken Ich werde "aaa" bekommen und wenn index[2] werde ich bekommen "ccc"

enter image description here

@IBOutlet weak var titleLable: UILabel! 
@IBOutlet weak var infoButton: UIButton! 

myCell.titleLable.text = self.arrayList[indexPath.row] 
myCell.infoButton.tag = indexPath.row 
myCell.infoButton.addTarget(self, action: "buttonClicked", forControlEvents: .TouchUpInside) 
+0

Ihre Aktion ist nicht mit Params, Pass-Taste, und dann von Button Tag können Sie "aaa" oder "Ccc" erhalten. – sanman

+0

Ich habe das schon gemacht, aber ich möchte den entsprechenden Text mit der Info-Schaltfläche anzeigen –

+0

Geben Sie das Tag an beide Schaltflächen von Storyboards und in Ihrer swift-Datei, dann überprüfen Sie, ob die Schaltfläche mit diesem Tag in if Bedingungen angeklickt und Array Elemente in Titeltext der Schaltfläche, die die Array-Eigenschaft von firstObject und lastObject verwendet. @SajjadAims – KAR

Antwort

1

Versuchen Sie, indexPath, wo die Schaltfläche mit der Schaltfläche Tag geklickt wird.

@IBAction func buttonClicked(sender:UIButton) { 
    let cell = tableView.cellForRowAtIndexPath(NSIndexPath.init(forRow: sender.tag, inSection: 0)) 
    cell.myLabel.text = arrayList[sender.tag] 
} 
1

müssen Sie wie

swift3

myCell.titleLable.text = self.arrayList[indexPath.row] 
myCell.infoButton.tag = indexPath.row 
myCell.infoButton.addTarget(self, action: #selector(yourVCName.buttonClicked(_:)), for: .touchUpInside) 

get Aktion als

@IBAction func buttonClicked(_ sender: UIButton){ 

    print(self.arrayList[sender. tag]) 

} 

Swift2

012 tun
myCell.titleLable.text = self.arrayList[indexPath.row] 
myCell.infoButton.tag = indexPath.row 
myCell.infoButton.addTarget(self, action: "buttonClicked:", forControlEvents: .TouchUpInside) 


@IBAction func buttonClicked(sender: UIButton){ 

    print(self.arrayList[sender. tag]) 

} 
+0

Wert der Schaltfläche haben keine Mitgliedsmarkierung, –

+0

Sie benötigen @IBOutlet schwach var infoButton: UIButton! –

+0

siehe http://stackoverflow.com/questions/33662020/in-custom-uitable-value-of-type-anyobject-has-no-member-tag-error –

1

in Ihrer Tabelle View-Controller

let dataSource: [String] = [ "aaa" , "bbb" , "ccc"] 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier(YourCellIdentifier, forIndexPath: indexPath) as! YourCell 
    let title = dataSource[indexPath.row] 
    cell.setup(withTitle: title, delegate: self) 
    return cell 
} 

// MARK: - Your Cell Delegate 
func didTapActionButton(fromCell cell: UITableViewCell) { 
    if let indexPath = itemTable.indexPathForCell(cell) { 
     let selectedItem = dataSource[indexPath.row] 
     print(selectedItem) 
    } 
} 

In Ihrem Tabellenansicht Handy

Zunächst definiert ein Protokoll:

protocol YourTableViewCellDelegate { 
    func didTapActionButton(fromCell cell: UITableViewCell) 
} 

Und dann:

// MARK: - Properties 
var delegate: YourTableViewCellDelegate? 

// MARK: - @IBOutlets 
@IBOutlet weak var titleLabel: UILabel! 
@IBOutlet weak var infoButton: UIButton! 

// MARK: - @IBActions 
@IBAction func buttonClicked(sender: UIButton) { 
    delegate?.didTapActionButton(fromCell: self) 
} 

// MARK: - Public Methods 
func setup(withTitle title: title, delegate: YourTableViewCellDelegate?) { 
    titleLabel.text = title 
    self.delegate = delegate 
} 
Verwandte Themen