我正在尝试在表格单元格中显示 WebView 。 “let webView = cell.viewWithTag(12) as! UIWebView” 处弹出错误展开可选内容时意外发现 nil。 webView的标签是12(交叉检查了很多次)。 dateLabel 和 titleLabel 工作正常。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell!
cell = tableView.dequeueReusableCellWithIdentifier("idCellPost", forIndexPath: indexPath)
let dateLabel = cell.viewWithTag(10) as! UILabel
let captionLabel = cell.viewWithTag(11) as! UILabel
let webView = cell.viewWithTag(12) as! UIWebView
dateLabel.text = "8/18/2015"
captionLabel.text = "Dummy text."
webView.loadRequest(getRequest())
webView.scalesPageToFit = true
return cell
}
最佳答案
您已创建自定义单元格。您不需要使用标签访问它的 subview 。您可以直接将它们作为属性访问。
if let customCellObject = tableView.dequeueReusableCellWithIdentifier("idCellPost", forIndexPath: indexPath) as? CustomCell {
customCellObject.dateLabel?.text = "8/18/2015"
customCellObject.captionLabel?.text = "Dummy text."
customCellObject.webView?.loadRequest(getRequest())
return customCellObject
}
当然,在 if block 之外,如果自定义单元格未出列,您需要处理这种情况。
关于ios - 在 TableView 中找不到 UIWebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32064783/