插入UITableViewCell拒绝在单元格选择上发生

时间:2015-04-19 23:53:53

标签: ios uitableview swift

我有一个非常具体的问题,我无法弄清楚。我一直在努力创建自己的Apple DateCell项目迭代。一个问题:当我点击第1行第0行的单元格时,我无法弄清楚为什么没有插入DatePickerCell。输出错误,我试图跟随它,但我确实只是想不出来。该应用程序在tableview.insertRowsAtIndexPaths()方法崩溃。

这是视图控制器:

import UIKit

class AddAssignmentViewController: UITableViewController {

    let cellTitles: [String] = ["Title", "Due Date", "Teacher"]
    var showDatePickerCell: Bool = false
    var showPickerCell: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - UITableViewDataSource Implementation

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        switch indexPath {
        case NSIndexPath(forRow: 1, inSection: 1):
            let cell = tableView.dequeueReusableCellWithIdentifier("DatePickerCell", forIndexPath: indexPath) as! DatePickerCell
            return cell
        case NSIndexPath(forRow: 1, inSection: 2):
            let cell = tableView.dequeueReusableCellWithIdentifier("PickerCell", forIndexPath: indexPath) as! PickerCell
            return cell
        default:
            let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
            cell.textLabel!.text = cellTitles[indexPath.section]
            return cell
        }
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        switch indexPath {
        case NSIndexPath(forRow: 0, inSection: 1):
            tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 1)], withRowAnimation: .Fade)
        case NSIndexPath(forRow: 0, inSection: 2):
            tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 1, inSection: 2)], withRowAnimation: .Fade)
        default:
            break
        }
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if showDatePickerCell && section == 1 {
            return 2
        } else if showPickerCell && section == 2 {
            return 2
        }

        return 1
    }

    // MARK: - UIViewController Implementation (ViewController Life Cycle)

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    }

}

在第1部分第0行点击单元格时出错:

2015-04-19 16:52:12.617 myHomework[6287:387908] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:1406
2015-04-19 16:52:12.620 myHomework[6287:387908] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 1 into section 1, but there are only 1 rows in section 1 after the update'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000103516c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000105081bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x0000000103516aca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00000001039b398f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   UIKit                               0x0000000103e915c8 -[UITableView _endCellAnimationsWithContext:] + 6907
    5   myHomework                          0x0000000102f175e0 _TFC10myHomework27AddAssignmentViewController9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 672
    6   myHomework                          0x0000000102f1790f _TToFC10myHomework27AddAssignmentViewController9tableViewfS0_FTCSo11UITableView23didSelectRowAtIndexPathCSo11NSIndexPath_T_ + 79
    7   UIKit                               0x0000000103ea6dc9 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1293
    8   UIKit                               0x0000000103ea6f0a -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
    9   UIKit                               0x0000000103dd962c _applyBlockToCFArrayCopiedToStack + 314
    10  UIKit                               0x0000000103dd94a6 _afterCACommitHandler + 533
    11  CoreFoundation                      0x0000000103449ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    12  CoreFoundation                      0x0000000103449c00 __CFRunLoopDoObservers + 368
    13  CoreFoundation                      0x000000010343fa33 __CFRunLoopRun + 1123
    14  CoreFoundation                      0x000000010343f366 CFRunLoopRunSpecific + 470
    15  GraphicsServices                    0x00000001074fda3e GSEventRunModal + 161
    16  UIKit                               0x0000000103db5900 UIApplicationMain + 1282
    17  myHomework                          0x0000000102f0d837 main + 135
    18  libdyld.dylib                       0x00000001057d9145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:1)

您的numberOfRowsInSection方法每次只返回1。现在,如果showPickerCellshowDatePickerCell为真,它将返回2,但它们始终设置为false。调用insertRowsAtIndexPaths时,它希望事后向数据源询问行数,并获得一个等于表视图中实际行数的值。

要解决此问题,您需要在将行插入表视图之前,将这些属性中的任何一个设置为true内的didSelectRowAtIndexPath