cell.contentView.viewWithTag 在 UITableView 中加载数据时给出 nil 值
cell.contentView.viewWithTag gives nil value while loading data in UITableView
我希望在 UITableView
的第一条记录上捕获图像点击事件,当用户点击时我 cell.imageAvtar
我只想捕获该事件。
这是我正在使用的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("details", forIndexPath: indexPath) as! AccountCell
if (indexPath.row == 0) {
(cell.contentView.viewWithTag(101) as! UIImageView).image = UIImage(named: "no_image_available.jpg")
}
return cell
}
但是 (cell.contentView.viewWithTag(101)
以 nil
的形式返回。我已经尝试 (cell.contentView.viewWithTag(100)
也尝试过(单元格。imageAvtar.viewWithTag(101)。
在界面生成器或情节提要中检查您的 imageView 标签,如果它是 0,则将其设为 101,然后重试..
你也可以检查
for subView in cell.contentView.subView {
print("subView tag --> \(subView.tag)!")
}
在你的 cellForRowAtIndexPath 中试试这个
试试,
cell.viewWithTag(101)
或 self.view.viewWithTag(101)
如果标签是唯一的(即如果您没有在其他地方使用此标签)。
第二件事你必须添加 gesture recognizer
来捕获事件。你怎么知道它是 returning nil ?它可能不是 return nil。你又犯了一个错误。确保 no_image_available.jpg
在项目中正确可用!
另一件事是确保您已正确设置标签。
我按照 vadian 和 jrturton 的建议使用了 IBOutlets
。
这是工作代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("details", forIndexPath: indexPath) as! AccountCell
if (indexPath.row == 0) { let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
cell.imageAvtar.userInteractionEnabled = true
cell.imageAvtar.addGestureRecognizer(tapGestureRecognizer)
}
}
func imageTapped(img: AnyObject)
{
print("event captured")
//your logic comes here
}
我希望在 UITableView
的第一条记录上捕获图像点击事件,当用户点击时我 cell.imageAvtar
我只想捕获该事件。
这是我正在使用的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("details", forIndexPath: indexPath) as! AccountCell
if (indexPath.row == 0) {
(cell.contentView.viewWithTag(101) as! UIImageView).image = UIImage(named: "no_image_available.jpg")
}
return cell
}
但是 (cell.contentView.viewWithTag(101)
以 nil
的形式返回。我已经尝试 (cell.contentView.viewWithTag(100)
也尝试过(单元格。imageAvtar.viewWithTag(101)。
在界面生成器或情节提要中检查您的 imageView 标签,如果它是 0,则将其设为 101,然后重试..
你也可以检查
for subView in cell.contentView.subView {
print("subView tag --> \(subView.tag)!")
}
在你的 cellForRowAtIndexPath 中试试这个
试试,
cell.viewWithTag(101)
或 self.view.viewWithTag(101)
如果标签是唯一的(即如果您没有在其他地方使用此标签)。
第二件事你必须添加 gesture recognizer
来捕获事件。你怎么知道它是 returning nil ?它可能不是 return nil。你又犯了一个错误。确保 no_image_available.jpg
在项目中正确可用!
另一件事是确保您已正确设置标签。
我按照 vadian 和 jrturton 的建议使用了 IBOutlets
。
这是工作代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("details", forIndexPath: indexPath) as! AccountCell
if (indexPath.row == 0) { let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
cell.imageAvtar.userInteractionEnabled = true
cell.imageAvtar.addGestureRecognizer(tapGestureRecognizer)
}
}
func imageTapped(img: AnyObject)
{
print("event captured")
//your logic comes here
}