- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 View Controller 中有一个 TableView
,在 UIViewController
中也有一个 UILabel
。一切都显示得很好,但我正在努力实现某些目标,但我不能简单地了解其背后的逻辑。
tableview 单元格有一个 UILabel,它在单元格上有 Int
值,这些数字现在不同 我如何获得 tableView 单元格中标签上的数字总和并显示在标签中 View Controller 。
我已经尝试在 UIView Controller 中创建一个变量并将这个值添加到 t 但我无法这样做,因为我无法将这个数字加在一起
有关如何执行此操作的任何帮助。谢谢
var total: Double?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! BrokageCheckoutCell
cell.configure()
total = Double("\(cell.subPrice.text!)")
cell.selectionStyle = .none
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: cellId2, for: indexPath) as! OtherCheckoutCell
cell.configure()
cell.selectionStyle = .none
return cell
default: break
}
return UITableViewCell()
}
最佳答案
你问:
how do I get the sum of the numbers on the Label in the tableView cell and display on the Label in the view controller
简而言之,你不知道。表格 View 单元格(“ View ”的一部分)中的标签不是我们的数据源。它仅用于显示单个字符串。
例如,假设您的应用有 100 个项目要求和,但应用的表格 View 一次只显示 12 行。作为内存和性能的改进,iOS 将重用滚动出 View 的单元格,以便显示滚动到 View 中的新行的内容。但这意味着你不能说“将这 100 个单元格中标签的所有内容相加”,因为没有 100 个单元格;只有 12 个。
您的应用程序应该引用它的“模型”,cellForRowAt
用来确定在给定的表格 View 单元格中显示什么的结构(例如,数组)。因此,如果您想求和一个总计,您应该将该数组中的值相加,而不是引用所有单元格。
如果您的单元格具有允许修改数据的控件,那么该单元格应该启动模型的更新。然后,计算总计(对数组中的值求和)的过程仍然有效。
那么,让我们考虑一个例子:
您应该有一个包含要求和的数字的模型。你的例子有点不清楚,所以我将创建一个例子,其中每个表行包含一个名称、一个单价和一个数量:
struct Item {
let name: String
let unitPrice: Decimal
var quantity: Decimal
}
在此示例中,任何给定行的总计将是数量乘以单价。所有行的总计将是所有这些产品的总和。
那么你的 View Controller 可能有一个数组用于它的模型:
var items: [Item] = ...
然后,当您想要更新总计标签时,您只需使用一种方法来计算每行总计的数量乘以价格,然后将这些值相加以计算总计。然后它将相应地更新总计文本字段:
private extension ViewController {
func updateTotal() {
let total = items
.map { $0.quantity * $0.unitPrice }
.reduce(0, +)
totalLabel.text = totalFormatter.string(for: total)
}
}
请注意,我不是从单元格中检索这些数字(因为它们可能会被重复使用),而是从模型中检索我需要的所有数据。
不过,这里的关键是,例如,如果您的单元格允许用户更改其中一个值,则您需要一种机制来更新 TableView Controller 的模型。我们将为此使用一个协议(protocol):
protocol ItemCellDelegate: class {
func cell(_ cell: ItemCell, didUpdateQuantity quantity: Decimal)
}
class ItemCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var quantityTextField: UITextField!
@IBOutlet weak var unitPriceLabel: UILabel!
static let quantityFormatter: NumberFormatter = ...
static let priceFormatter: NumberFormatter = ...
weak var delegate: ItemCellDelegate?
}
显然,当您配置单元格时, View Controller 将指定用于更新文本字段(或其他内容)的委托(delegate):
// MARK: Public methods
extension ItemCell {
func configure(name: String, quantity: Decimal, unitPrice: Decimal, delegate: ItemCellDelegate) {
self.delegate = delegate
nameLabel.text = name
quantityTextField.text = ItemCell.quantityFormatter.string(for: quantity)
unitPriceLabel.text = ItemCell.priceFormatter.string(for: unitPrice)
}
}
当 ItemCell
获得更新时,它只调用委托(delegate):
// MARK: Private methods
private extension ItemCell {
@IBAction func didUpdateQuantity(_ sender: UITextField) {
var quantity: Decimal?
if let text = sender.text, let value = ItemCell.quantityFormatter.number(from: text) {
quantity = value.decimalValue
}
delegate?.cell(self, didUpdateQuantity: quantity ?? 0)
}
}
然后,当 View Controller 配置单元格时,它将自己提供为委托(delegate):
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath) as! ItemCell
let item = items[indexPath.row]
cell.configure(name: item.name, quantity: item.quantity, unitPrice: item.unitPrice, delegate: self)
return cell
}
}
当然, View Controller 将遵守该协议(protocol)以接受对文本字段的更新并更新模型:
extension ViewController: ItemCellDelegate {
func cell(_ cell: ItemCell, didUpdateQuantity quantity: Decimal) {
guard let indexPath = tableView.indexPath(for: cell) else { return }
items[indexPath.row].quantity = quantity
updateTotal()
}
}
而且,如您所见,如果可以的话,在更新模型后,它也可以自动更新总数。
这里的关键是我们从不将单元格用作我们保存数据的地方。这些单元格仅用于 (a) 显示数据和 (b) 通知 View Controller 是否需要注意任何更新。
我们努力实现责任的明确分离,其中“ View ”用于显示当前屏幕上的控件并与之交互,而“模型”包含我们所有的数据。
关于ios - 快速求和 UITableView 单元格上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717127/
我坚持我的项目(用 Swift 编写) 我有一个 UITableView,它有一个字符串,它提供了表格中的十个类别。我想做的是选择这些类别中的一个来触摸它,然后打开一个包含其他类别的 secondTa
好的,这从 iOS 7 Beta 6 开始就被打破了。 在启动时,我将表格 View 的背景更改为图像(如果我将其设置为颜色,它会做同样的事情)并且表格单元格是白色的...... 当我选择一个单元格时
我有一个包含不同部分的 UITableView,每个单元格上都有一个图像。因为图像是为每个单元格单独下载的,所以我正在寻找一种延迟加载解决方案。 在 iOS Developer Library 上,它
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
可以在UIRefreshControl的底部添加UITableView吗? 我会用它来加载更多数据。 拜托,有什么建议吗? 最佳答案 我相信不会有任何简单的解决方案来解决这个问题。可能有人可以编写一个
我有一个 UITableView 和一个使用 Swift 的自定义 UITableViewCell。当我设置 self.MyTable.editing = true 时,我看到该行上有删除图标,并且似
我为 iPhone 创建了一个下载器应用程序。我有用于显示主动/被动下载的表格 View 。在 tableview 的自定义单元格中有进度条,它显示 %XX 下载状态。现在我正在使用 NSTimer
我正在编写 UITableView 的自定义子类。我需要这个对象本身成为它自己的数据源和委托(delegate),然后这个子类将有它自己的数据源和委托(delegate)。这主要是为了让我可以拦截对数
我有一个问题已经困扰我几天了。 我有一个名为 countries 的数组。该数组包含一个自定义对象,其中包含国家、城市、IP、状态等数据。 我需要在按属性“国家/地区”划分的表格 View 中显示这些
我有一个 UITableViewController,目前显示 4 个将始终显示的单元格,并且想将它们组合在一起,但我不知道如何操作。当在 UIViewController 或类似的东西上插入标准 U
我以编程方式创建了一个 UITableView,现在我想向它添加一个固定的标题。 正如我下面的代码一样,标题也随着我的 tableview 滚动。 UILabel *label = [[[UI
我正在创建一个 UITableViewController(在 UINavigationController 的根部)并在另一个 View Controller 之上以模态方式呈现它。我让它在一定程度
我有问题。我有动态 TableView 。所以我需要在我的 textLabel 中更改文本大小。我试过: 一个: cell.textLabel.font = [UIFont systemFontOfS
iOS 8 中的新功能是 separatorEffect属性,您可以为其分配 UIVisualEffect。有没有人知道这是做什么用的?我试过了,我不认为它有任何,呃,视觉效果。 最佳答案 我想知道完
今天早上,我刚刚安装了包含iOS 6的新Xcode。 我有一个加载了包含章节和行的plist文件的表格 View 。各章定义各节。 用户选择章节和行,并将tableview自动滚动到正确的位置(在vi
我在iOS 7上启动VoiceOver时遇到了一些麻烦。我确实在UITableViewHeaderFooterView中注册了viewDidLoad子类的实例,但是当我用-[tableView deq
我在 UITableView 中添加了一些数据,我想更改单元格中显示的文本的颜色。 是否有委托(delegate)方法可以执行此操作,或者有其他方式吗? 最佳答案 无需标签 您只需要编码: cell.
是否可以在滚动 UITableView 时动态更改单元格的高度? 当用户将其向下拖动到顶部时,滚动位置到达某个点时,我需要更改多个单元格的高度。我可以通过发出 reloadData 成功地做到这一点,
我正在使用 iOS 5,并在表格中动态生成单元格(2 个部分,每个部分 3 行)。每个部分都有一个标题,该标题也是使用 titleForHeaderInSection 调用动态生成的。 我还设置了一个
我正在寻找 UITableView 的回调函数当我使用 Apple Remote 导航并按向上或向下键选择行时(而不是按 Enter 键时),当该行聚焦时。 在 iOS 上,我们有 didSelect
我是一名优秀的程序员,十分优秀!