2016-09-27 5 views
2

У меня есть табличный вид, где я создал ярлык и две кнопки. Я застреваю при получении текста с ярлыка при нажатии кнопки. Я создал список массива, как:View tableview cell text on table cell cell

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

Я хочу, если я нажму на кнопку index[0] попаду «ааа», и если index[2] попаду «КТС»

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

Ваше действие не с параметрами, переход к нему, а затем с помощью тега кнопки вы можете получить «aaa» или «ccc». – sanman

+0

Я уже это сделал, но я хочу отобразить соответствующий текст с помощью кнопки info, нажав кнопку –

+0

, чтобы дать тег обеим кнопкам из раскадровки и в вашем быстром файле, а затем проверить, нажата ли кнопка с этим тегом, если условия и чем назначить элементы массива в текст заголовка кнопки с использованием свойства массива firstObject и lastObject. @SajjadAims – KAR

ответ

1

Попытайтесь получить indexPath, где нажата кнопка с помощью тега кнопки.

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

вам нужно сделать, как

swift3

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

прибудете действие, как

@IBAction func buttonClicked(_ sender: UIButton){ 

    print(self.arrayList[sender. tag]) 

} 

Swift2

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

значение кнопки не имеет тега участника, –

+0

Вам нужно @IBOutlet weak var infoButton: UIButton! –

+0

см. Это http://stackoverflow.com/questions/33662020/in-custom-uitableview-value-of-type-anyobject-has-no-member-tag-error –

1

в таблице 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) 
    } 
} 

В вашем Table View Cell

Во-первых, определить протокол:

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

И потом:

// 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 
}