ios - 通过 Swift 中的标签访问 UITableViewCell 内的不同 View

标签 ios uitableview swift xcode6

我正在尝试使用 swift 为 iOS 8 制作一个应用程序。这里的目标是制作一种新闻提要。此提要显示用户的帖子,遵循特定模式。

我想到了使用 UITableView,其中每个单元格都遵循自定义布局。当我尝试访问其中的文本标签时出现问题。我尝试通过它的标签访问它,但是当我这样做时,整个应用程序崩溃了。报告的错误是 “Swift dynamic cast failed”,我正在使用以下代码访问 View :

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {

    let cellId: String = "cell"

    var cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellId) as UITableViewCell

    if let ip = indexPath{
        cell.textLabel.text = myData[ip.row] as String
        var lbl = cell.contentView.viewWithTag(0) as UILabel
        lbl.text = "ola"
    }

    return cell
}

我做错了什么吗?提前致谢!

最佳答案

我认为问题出在标记 0。所有 View 都使用默认值 0。因此请尝试另一个标记值。

关于ios - 通过 Swift 中的标签访问 UITableViewCell 内的不同 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554364/

相关文章:

ios - UITableView 中多张图片的延迟加载

ios - 如何在 UITextField keyboardType NamePhonePad 上设置默认启动备用键盘 View

iphone - iOS 游戏玩家 ID

ios - UICollectionReusableView 标题中的 UIButtons

ios - performselector afterdelay 不起作用

ios - 无法在 iOS 10 中使用分布式配置文件验证企业应用程序

ios - 在 FirstVC 中选择单元格后,如何为 SecondVC 中的每个单元格调用按钮操作?

ios - 将新部分插入 UITableView 导致崩溃

ios - 用于 CFunctionPointer 到 Swift 闭包的 Objective-C 包装器

ios - 以编程方式更改方向不起作用