ios - 在 TableView 中找不到 UIWebView

标签 ios swift uitableview uiwebview

我正在尝试在表格单元格中显示 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/

相关文章:

ios - Objective c-Symbolication 问题 Error : "DEVELOPER_DIR" is not defined at ./symbolicatecrash line 60

swift - 将依赖项传递给选项卡栏 View Controller

ios - 单击按钮时如何防止查看 View (Swift)

ios - 将 495 单元格导入 TableView

ios - 我实现了 editActionsForRowAtIndexPath 和 commitEditingStyle 但在滑动单元格时 tableViewCell 上没有出现任何编辑操作

ios - 无法在 AFNetworking AFHTTPRequestOperationManager 中设置内容类型

iOS 重复调用 CGContext.save/restoreGState(又名 CGContextSave/RestoreGState)导致锯齿

ios - Xcode 7 和部署目标 8.0

xcode - 为什么 swift 中的 self 有时颜色不同?

ios - 使带有删除按钮的 UITableView 对所有单元格可见