ios - Swift:初始化 UITableViewCells 时出现 NSRangeException

标签 ios swift uitableview nsrangeexception

我在尝试让我的 UITableView 正常运行时遇到了一些小问题。这是错误的样子:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'

我已将问题缩小到这个特定的代码片段:(第 2 行)

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(String(TestCell), forIndexPath: indexPath)
    return cell
}

完整的 Swift 文件:

class ViewController: UITableViewController {

let closeHeight: CGFloat = 91
let openHeight: CGFloat = 166
var itemHeight = [CGFloat](count: 4, repeatedValue: 91.0)


override func viewDidLoad() {
    super.viewDidLoad()

    tableView.registerClass(TestCell.self, forCellReuseIdentifier: String(TestCell))
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return itemHeight.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(String(TestCell), forIndexPath: indexPath)
    return cell
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return itemHeight[indexPath.row]
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! FoldingCell

    var duration = 0.0
    if itemHeight[indexPath.row] == closeHeight { // open cell
        itemHeight[indexPath.row] = openHeight
        cell.selectedAnimation(true, animated: true, completion: nil)
        duration = 0.5
    } else {// close cell
        itemHeight[indexPath.row] = closeHeight
        cell.selectedAnimation(false, animated: true, completion: nil)
        duration = 1.1
    }

    UIView.animateWithDuration(duration, delay: 0, options: .CurveEaseOut, animations: { () -> Void in
        tableView.beginUpdates()
        tableView.endUpdates()
        }, completion: nil)

    }
}

编辑 1:

class TestCell: FoldingCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    containerView = createContainerView()
    foregroundView = createForegroundView()

    // super class method configure views
    commonInit()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func animationDuration(itemIndex: NSInteger, type: AnimationType) -> NSTimeInterval {

    // durations count equal it itemCount
    let durations = [0.33, 0.26, 0.26] // timing animation for each view
    return durations[itemIndex]
}
}

extension TestCell{

func createForegroundView() -> RotatedView{
    let foregroundView = RotatedView(frame: .zero)
    foregroundView.backgroundColor = UIColor.blackColor()
    foregroundView.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(foregroundView)

    foregroundView.topAnchor.constraintEqualToAnchor(foregroundView.superview?.topAnchor, constant: 25)
    foregroundView.leftAnchor.constraintEqualToAnchor(foregroundView.superview?.leftAnchor, constant: 25)
    foregroundView.widthAnchor.constraintEqualToAnchor(foregroundView.superview?.widthAnchor, constant: -50)
    foregroundView.heightAnchor.constraintEqualToConstant(100)

    let topConstraint = foregroundView.constraints[0]
    topConstraint.identifier = "ForegroundViewTop"
    foregroundView.layoutIfNeeded()
    return foregroundView
}

func createContainerView() -> UIView {
    let containerView = UIView(frame: .zero)
    containerView.backgroundColor = UIColor.grayColor()
    containerView.translatesAutoresizingMaskIntoConstraints = false

    contentView.addSubview(containerView)

    containerView.topAnchor.constraintEqualToAnchor(containerView.superview?.topAnchor, constant: 25)
    containerView.leftAnchor.constraintEqualToAnchor(containerView.superview?.leftAnchor, constant: 25)
    containerView.widthAnchor.constraintEqualToAnchor(containerView.superview?.widthAnchor, constant: -50)
    containerView.heightAnchor.constraintEqualToConstant(100)

    let topConstraint = containerView.constraints[0]
    topConstraint.identifier = "ContainerViewTop"
    containerView.layoutIfNeeded()

    return containerView
}
}

仅供引用:

我没有使用默认的 Interface Builder,我已经在 TestCell.swift 文件中编写了所有与 GUI 相关的代码。

对可能导致此问题的原因有任何想法吗?

谢谢!

最佳答案

我敢打赌问题出在:

let topConstraint = containerView.constraints[0]

检查约束大小可以很容易地证明...:)

在创建约束时忘记调用约束的 active 方法,这不会将它们添加到约束列表中

即:

containerView.heightAnchor.constraintEqualToConstant(100).active = true

关于ios - Swift:初始化 UITableViewCells 时出现 NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38867367/

相关文章:

ios - 使用 SwiftCharts 框架时图形 View 超出范围

ios - 为什么对象不附加到数组?

ios - 未调用 UITableViewDelegate cellForRowAtIndexPath

ios - CoreGraphics (drawRect) 用于在 UITableViewCell 中绘制标签和 UIImageView

ios - 在iOS上使用phonegap

ios - 弹出 View Controller 到一个不存在的 View Controller

ios - 在 iOS 中的导航栏正下方显示标签

ios - 子类 UITextField 在使用具有自定义字体的尺寸类时覆盖字体

ios - Swift 4 将图像添加到图像数组

ios - 苹果手机 x : keep search bar within safe zone