2014-10-13 15 views
5

Tôi đã cố thay đổi màu nền của nút vuốt để xóa trong chế độ xem bảng của tôi. Tôi đã xem xét một số ví dụ Objective-C nhưng tôi đã không thể dịch chúng thành Swift.Thay đổi màu nền vuốt để xóa trong Swift

Đây là mã của tôi vào lúc này:

var cellDeleteBackground = UIView() 
    cellDeleteBackground.backgroundColor = UIColor.greenColor() 
    cell.editingAccessoryView = cellDeleteBackground 

Tôi có điều này trong cellForRowAtIndexPath nhưng tại thời điểm này nó được đâm với lỗi 'chấm dứt với ngoại lệ còn tự do kiểu NSException'

mã này có thể được can thiệp?

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    if tableView == self.searchDisplayController?.searchResultsTableView { 
     return false 
    } else { 
     return true 
    } 
} 

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.Delete) { 
      if tableView == self.searchDisplayController?.searchResultsTableView { 
       UITableViewCellEditingStyle.None 
      } else { 
      var valueToRemove: AnyObject! = unformatted.objectAtIndex(indexPath.row) 

      if images.objectAtIndex(indexPath.row) as NSObject == 0 { 
       totalSpendingsCounter = totalSpendingsCounter - Double(valueToRemove as NSNumber) 
       NSUserDefaults.standardUserDefaults().setDouble(totalSpendingsCounter, forKey: "spendingsCounter") 
      } else if images.objectAtIndex(indexPath.row) as NSObject == 1 { 
       totalCreditCounter = totalCreditCounter - Double(valueToRemove as NSNumber) 
       NSUserDefaults.standardUserDefaults().setDouble(totalCreditCounter, forKey: "creditCounter") 
      } 

      currencyDouble = NSUserDefaults.standardUserDefaults().doubleForKey("currencyCounter") 
      currentBudgetCalculation = currencyDouble + totalCreditCounter - totalSpendingsCounter 

      newTransactionEntered = true 

      var formatter = NSNumberFormatter() 
      formatter.numberStyle = .CurrencyStyle 
      formatter.locale = NSLocale.currentLocale() // This is the default 
      var formattedNumberCurrent = formatter.stringFromNumber(currentBudgetCalculation) 

      var defaults = NSUserDefaults(suiteName: "group.AffordIt") 
      defaults.setObject(formattedNumberCurrent, forKey: "currentBudgetWidget") 
      defaults.setObject(newTransactionEntered, forKey: "new") 

      values.removeObjectAtIndex(indexPath.row) 
      images.removeObjectAtIndex(indexPath.row) 
      names.removeObjectAtIndex(indexPath.row) 
      dates.removeObjectAtIndex(indexPath.row) 
      unformatted.removeObjectAtIndex(indexPath.row) 
      notes.removeObjectAtIndex(indexPath.row) 

      NSUserDefaults.standardUserDefaults().setObject(names, forKey: "names") 
      NSUserDefaults.standardUserDefaults().setObject(values, forKey: "values") 
      NSUserDefaults.standardUserDefaults().setObject(dates, forKey: "dates") 
      NSUserDefaults.standardUserDefaults().setObject(unformatted, forKey: "unformatted") 
      NSUserDefaults.standardUserDefaults().setObject(images, forKey: "images") 
      NSUserDefaults.standardUserDefaults().setObject(notes, forKey: "notes") 

      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
     } 
    } 
} 

Trả lời

8

Đây là cách bạn có thể tùy chỉnh swipe-to-delete trong iOS8:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { 
    var deleteButton = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in 
     self.tableView.dataSource?.tableView?(
      self.tableView, 
      commitEditingStyle: .Delete, 
      forRowAtIndexPath: indexPath 
     ) 

     return 
    }) 

    deleteButton.backgroundColor = UIColor.blackColor() 

    return [deleteButton] 
} 
+0

Cảm ơn câu trả lời tuy nhiên điều này cũng dường như làm hỏng ứng dụng của tôi với cùng một lỗi. Tôi đã cập nhật câu hỏi của mình với một số mã khác trong trường hợp đó là vấn đề. – user3746428

+0

Thông tin thêm về ngoại lệ là cần thiết. Đảm bảo bạn có điểm ngắt về ngoại lệ: http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions – Kirsteins

+0

Chỉ ra rằng nó không liên quan đến mã bạn đã đăng và nó hoạt động khỏe. Cảm ơn rất nhiều! – user3746428

1

Dưới mã cập nhật cho Swift 4, Xcode 9:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in 
     self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath) 
     return 
    } 
    deleteButton.backgroundColor = UIColor.black 
    return [deleteButton] 
} 
Các vấn đề liên quan