我有一些小问题试图让我的UITableView正常运行。这就是错误的样子:
*由于未捕获的异常终止应用' NSRangeException',原因:' * - [__ NSArray0 objectAtIndex:]:索引0超出空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相关的代码。
关于可能导致此问题的任何想法?
谢谢!
答案 0 :(得分:2)
我打赌问题来自:
let topConstraint = containerView.constraints[0]
检查约束大小可以很容易地证明......:)
您在创建约束时忘记在约束上调用活动方法,并且不会将它们添加到约束列表中
即:
containerView.heightAnchor.constraintEqualToConstant(100).active = true