- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于这个问题有几个问题。我已经尝试了所有建议的答案,但还没有任何效果。所以我有这个 func
用户收到的拒绝报价。我可以拒绝报价并删除单元格行,但是当只剩下一个单元格时,我得到 fatal error: Index out of range
func declineButtonTapped(sender: AnyObject) {
self.view.userInteractionEnabled = false
let buttonRow = sender.tag // this is the tag from my custom cell button
let offer = offers[buttonRow] // I get the error here
let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = "declining offer...."
let myUrl = NSURL(string: "\(ipAddress)/api/v1.0/offers.php")
let request = NSMutableURLRequest(URL: myUrl!)
request.HTTPMethod = "POST"
let postString = "id=\(offer.id!)&action=decline&offer_id=\(offer.offer_id!)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{ data, response, error in
if error != nil {
let messageToDisplay = error
self.view.userInteractionEnabled = true
return
}
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
if let parseJSON = json{
let resultValue = parseJSON["status"] as? String
if resultValue == "Success"{
dispatch_async(dispatch_get_main_queue()) {
print("before count is \(self.offers.count)") // before the error the count is 2 here
self.offers.removeAtIndex(buttonRow) //update my model
self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: buttonRow, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade)
print("after count is \(self.offers.count)") //then the count is 1 here
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
self.view.userInteractionEnabled = true
}
}else{
//no success
}
}
} catch{
}
}
task.resume()
}
内部cellForRowAtIndexPath
我为按钮分配标签值
offerCell.declineButton.tag = indexPath.row
offerCell.declineButton.addTarget(self, action: #selector(OpenDealsDetailsViewController.declineButtonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)
******更新*****
我想我发现了错误。当我打印 print("button row is\(buttonRow)")
号码没有更新。所以它第一次调用正确的行但第二次它保留它在 declineButtonTapped
时的 indexPath.row。第一次被调用
button row is0
before count is 2
after count is 1
button row is1 // this of course should be 0 as there is only one cell left
fatal error: Index out of range
如果我尝试做
self.offers.removeAtIndex(buttonRow)
self.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: buttonRow, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Fade)
self.tableView.reloadData()
我收到以下错误:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
最佳答案
您要么不应该使用标签,要么在每次更改后完全重新加载表格 View 。或者,您可以迭代可见单元格并更新其标记值。
如果您总是并且只删除表中的最后一行,那就没问题了。一旦删除较早的行,该行之后的所有行现在都具有不正确的标记值。因此,当您转到最后一行时,该标记中实际上并不存在该行,并且任何其他无效行将导致您从服务器中删除错误的项目。
更好的方法是向单元格传递一个类的实例,该类可以执行删除操作并回调 View Controller 并提供更新的详细信息。然后 View Controller 可以更新其数据源和 TableView 。通过这种方式,您可以将表索引路径与要对数据执行的操作分开。
关于ios - 多次调用 deleteRowsAtIndexPaths,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36247084/
当我从表中删除行时,我的应用程序崩溃了。这是我检测到错误和堆栈跟踪的来源。谢谢! //delete row from database - (void)tableView:(UITableView *
我对 Swift 和 iOS 比较陌生,我有一个问题 - 我有一个扩展 UITableViewCell 的自定义 SwipeCell 类。这是一个可以左右滑动的单元格,每侧都有按钮。出于某种原因,如果
关于这个问题有几个问题。我已经尝试了所有建议的答案,但还没有任何效果。所以我有这个 func用户收到的拒绝报价。我可以拒绝报价并删除单元格行,但是当只剩下一个单元格时,我得到 fatal error:
我正在尝试借助 deleteRowsAtIndexPaths 折叠和展开 UITableView 部分。似乎什么都没有发生,但我不明白为什么。 NSMutableArray *tmpArray
我正在尝试使用以下代码删除。 [super deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade
尝试成为 ios7 风格,我在点击表格中的单元格时将 UIPickerView 插入到 UITableView 中。这工作正常并且动画效果很好。但是,当我通过调用 deleteRowsAtIndexP
这个问题在这里已经有了答案: crash on deleteRowsAtIndexPaths (5 个答案) 关闭 7 年前。 我试图在稳定 View 中删除一行,但每次我在应用程序中选择删除时它都
我在IB中设计了UIView,并在其上设计了UITableView。在 View Controller 的 viewDidLoad 方法中,我初始化自定义 UITableViewContoller 并
当我尝试将 deleteRowsAtIndexPaths 与动画 UITableViewRowAnimationTop 一起使用时,我要删除的单元格覆盖了上面的单元格(在动画中),然后消失。我做了一个
当我尝试将 deleteRowsAtIndexPaths 与动画 UITableViewRowAnimationTop 一起使用时,我要删除的单元格覆盖了上面的单元格(在动画中),然后消失。我做了一个
我目前有一个包含多个部分的表格 View 。每个部分包含特定日期的数据。每个部分都有一个标题,说明数据是在多少天前输入的。 当使用deleteRowsAtIndexPath删除行时,当该部分中的所有行
我正在尝试很好地删除 mu UITableView 的单元格: - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITab
我不太确定这里发生了什么,但我遇到了一个我正在使用的联系表。在我的表单中,我添加了向联系人添加电子邮件地址的功能。如下图所示,一旦用户点击“添加电子邮件”,一行就会添加到“电子邮件”部分。 但是,在我
这个问题在这里已经有了答案: crash on deleteRowsAtIndexPaths (5 个答案) 关闭 7 年前。 当我从 tableView 中删除一行时发生崩溃。不确定发生了什么。这
我有一个 UITableView 和一个在后台运行的调度程序,从底层数据对象中删除记录。每次删除对象时,我都希望 TableView 相应地更新。 但是当我尝试从 TableView 中删除一行时,我
我想知道是否有可能捕捉到在 deleteRowsAtIndexPaths 期间触发的动画是否结束?我想要做的是从 UITableView 中删除一些所有行,除了我点击的行。接下来我想在同一个 UITa
我在 Swift、iOS 8、Xcode 6 Beta 6 中从我的 tableView 中删除一行时遇到了一些麻烦。每次我尝试删除一行时,我都会收到类似 的错误 Assertion failure
我有一个可能有几百行的表格,所有行的高度都不同。每当用户删除一行时,调用: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnim
我有一个动态的 TableView,想用动画删除行。 我的代码: struct TableItem { let text: String let id: String let
我正在尝试从 deleteRowsAtIndexPaths 更新我的 tableView 以使用 animation.fade 。所以请你告诉我如何在我有两个数组时更新 tableView? func
我是一名优秀的程序员,十分优秀!