我不知道发生了什么,我将button.tag
设置为表格行,当它到达行&gt; 1,它会抛出lldb。它适用于button.tag <= 1
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cells")! as UITableViewCell
let alertBtn = cell.viewWithTag(1) as! UIButton;
alertBtn.tag = indexPath.row
alertBtn.addTarget(self, action: Selector(("showAlert:")), for: UIControlEvents.touchUpInside)
return cell
}
答案 0 :(得分:1)
应用程序在此行崩溃,因为它无法找到带有标记1的视图,标记正在每个具有行值的单元格中更新。
let alertBtn = cell.viewWithTag(1) as! UIButton
删除此行并将 @IBOutlet 用于alertBtn来自 UITableViewCell ,而不是使用标记进行刷新。
答案 1 :(得分:0)
如果你想获得包含tapped按钮的单元格的indexPath,你可以使用与你的要求类似的功能。
func showAlert(sender: AnyObject) {
if let cell = sender.superview?.superview as? UITableViewCell{ // do check your viewchierarchy in your case
let indexPath = itemTable.indexPath(for: cell)
}
print(indexPath)// you can use this indexpath to get index of tapped button
}
从cellForRowAtIndexPath alertBtn.tag = indexPath.row
如果您可以将Custom Cell用于此目的,您可以像以前一样获得所选按钮的索引路径。
创建CustomCell并为您的按钮和标签等创建IBOutlet。您可以在cellForRowAtIndexPath中访问单元格的子视图,并为您的按钮分配标签。如果您对CustomCell有任何疑问,请告诉我。
答案 2 :(得分:0)
Swift 3X ......
您正在替换您的代码,因此第一个代码项目将变为零,因此请替换此代码...
if(isset($_POST['search_code']) && !empty($_POST['search_code']))
{
$search_code = $_POST['search_code'];
global $wpdb;
$helloworld_id = $wpdb->get_var("SELECT s_image FROM search_code WHERE s_code = $search_code");
if (empty($helloworld_id)) {
echo '<div class="no_results">No results found</div>';
}else{ ?>
<img src="http://igtlaboratories.com/wp-content/uploads/images/<?php echo $helloworld_id; ?>"style="width:200px;height: auto;">
<?php }
}
答案 3 :(得分:0)
尝试自定义UITableViewCell
。
为您的新班级声明协议和委托。连接一个动作并调用委托
protocol MyCellDelegate: class {
func buttonPressed(for cell: MyCell)
}
class MyCell:UITableViewCell {
weak var delegate: MyCellDelegate?
@IBAction func buttonPressed(sender: Any){
self.delegate?.buttonPressed(for: self)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
.......
cell.delegate = self
........
}
请记住向您的VC添加新的协议实现。您可以添加prepareForReuse
方法,并在重复使用单元格时将委托重置为nil。