我有一个 UITableView。当我点击“编辑”按钮时,它会进入编辑模式,但每个单元格上的红色“-”按钮不会显示“删除”按钮。
这是我为此使用的代码:
//
// ViewController.swift
// Measured Blood Lost
//
// Created by Josh Birnholz on 4/11/16.
// Copyright © 2016 Josh Birnholz. All rights reserved.
//
import UIKit
class MainViewController: UITableViewController, weightCellDelegate {
@IBOutlet weak var totalWeightBarButtonItem: UIBarButtonItem!
private var totalWeightLabel = UILabel(frame: CGRectZero)
@IBOutlet weak var totalWeightTextField: UITextField!
var totalWeight: Int = 0
let availableWeights: [Weight] = [Weight(name: "Lap", weightInGrams: 22),
Weight(name: "Lap counting bag", weightInGrams: 25),
Weight(name: "Sterile green towel", weightInGrams: 90),
Weight(name: "Sterile blue towel", weightInGrams: 55),
Weight(name: "Kick bucket red bag", weightInGrams: 50),
Weight(name: "Medium red bag", weightInGrams: 70),
Weight(name: "Large red bag", weightInGrams: 120),
Weight(name: "Washcloth", weightInGrams: 30),
Weight(name: "Towel", weightInGrams: 192),
Weight(name: "Blue cloth underpad", weightInGrams: 340),
Weight(name: "Regular patient gown", weightInGrams: 340),
Weight(name: "XL patient gown", weightInGrams: 498),
Weight(name: "Under buttocks drape", weightInGrams: 76),
Weight(name: "Mini lap", weightInGrams: 6),
Weight(name: "Vag packing", weightInGrams: 16),
Weight(name: "Pink peri pad", weightInGrams: 26),
Weight(name: "Large white (Capri +)", weightInGrams: 40)
]
var weights = [Weight]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
totalWeightLabel.backgroundColor = UIColor.clearColor()
totalWeightLabel.textAlignment = .Center
totalWeightBarButtonItem.customView = totalWeightLabel
navigationItem.leftBarButtonItem = editButtonItem()
updateTotalWeightLabel()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return weights.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("WeightCell", forIndexPath: indexPath) as! WeightCell
let name = weights[indexPath.row].name
let weightInGrams = weights[indexPath.row].weightInGrams
let quantity = weights[indexPath.row].quantity
if let nameLabel = cell.viewWithTag(100) as? UILabel {
nameLabel.text = "\(name.pluralize(quantity))"
nameLabel.textColor = quantity == 0 ? UIColor.lightGrayColor() : UIColor.blackColor()
}
if let weightLabel = cell.viewWithTag(101) as? UILabel {
weightLabel.text = "-\(weightInGrams * quantity) gm"
}
if let quantityTextField = cell.viewWithTag(102) as? UITextField {
quantityTextField.text = quantity == 0 ? "" : String(quantity)
cell.delegate = self
}
return cell
}
func quantityChanged(cell: WeightCell, newQuantity: Int, actionSender: AnyObject) {
let point = actionSender.convertPoint(CGPointZero, toView: tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(point)!
weights[indexPath.row].quantity = newQuantity
tableView.reloadData()
updateTotalWeightLabel()
}
@IBOutlet weak var addButton: UIBarButtonItem!
@IBAction func addButtonPressed(sender: AnyObject) {
addItems([randomWeight(), randomWeight()])
updateTotalWeightLabel()
}
let nameTextField = UITextField()
let weightTextField = UITextField()
func randomWeight() -> Weight {
let randomWeight = availableWeights[Int(arc4random_uniform(UInt32(availableWeights.count)))]
let quantity = Int(arc4random_uniform(5))
return Weight(name: randomWeight.name, weightInGrams: randomWeight.weightInGrams, quantity: quantity)
}
func addItems(weightsToAdd: [Weight]) {
for weightToAdd in weightsToAdd {
print("weightToAdd: \(weightToAdd.quantity) \(weightToAdd.name.pluralize(weightToAdd.quantity))")
if weightToAdd.quantity > 0 {
var foundIndex: Int?
var index = 0
for presentWeight in weights {
if presentWeight.name == weightToAdd.name {
foundIndex = index
}
index += 1
}
if foundIndex == nil {
weights.append(weightToAdd)
let indexPath = NSIndexPath(forRow: weights.count-1, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
} else {
weights[foundIndex!].quantity += weightToAdd.quantity
tableView.reloadData()
}
}
}
}
@IBAction func totalWeightTextFieldEditingBegan(sender: AnyObject) {
if totalWeightTextField.text!.hasSuffix(" gm") {
totalWeightTextField.text = totalWeightTextField.text!.substringToIndex(totalWeightTextField.text!.endIndex.advancedBy(-3))
}
}
@IBAction func totalWeightTextFieldEditingEnded(sender: AnyObject) {
totalWeightTextField.text!.numericize()
if totalWeightTextField.text! == "" {
totalWeight = 0
} else {
if let totalWeightInt = Int(totalWeightTextField.text!) {
totalWeight = totalWeightInt
} else {
totalWeight = Int.max
}
totalWeightTextField.text = "\(String(totalWeight)) gm"
}
updateTotalWeightLabel()
}
func updateTotalWeightLabel() {
var measuredBloodLost = totalWeight
for weight in weights {
measuredBloodLost = measuredBloodLost - (weight.weightInGrams * weight.quantity)
}
if measuredBloodLost < 0 {
totalWeightLabel.textColor = UIColor.redColor()
} else {
totalWeightLabel.textColor = UIColor.blackColor()
}
totalWeightLabel.text = "Measured Blood Lost: \(measuredBloodLost) mL"
totalWeightLabel.sizeToFit()
}
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Remove data
weights.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
updateTotalWeightLabel()
}
}
滑动单元格确实会出现删除按钮,它会正确删除单元格和数据,所以我不知道我做错了什么!
谢谢。
最佳答案
好的,问题出在您的导航 Controller 中的点按手势识别器。如果你想保留它,你可以使用类似下面的东西:
class NavigationController : UINavigationController, UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(NavigationController.dismissKeyboard))
tap.delegate = self
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if let view = touch.view where String(view.dynamicType) == "UITableViewCellEditControl" {
print("do not receive touch")
return false
}
print("do receive touch")
return true
}
}
关于ios - 滑动删除作品,但删除按钮没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36583644/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle