- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表格 View ,里面装满了水果、蔬菜和肉类。当我使用 UIPickerView 更改类别时,我在这一点上遇到了问题,我以前保存在数组中的数据被删除了。
我不知道如何处理这个问题。我将如何保存我之前的类别数据以及用户更改类别时的数据?
下面是我正在处理的一些代码。
CellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "adddiseasefoodcell", for: indexPath) as! AddDiseaseTableViewCell
print(indexPath.row)
cell.foodname.text = foodname[indexPath.row]
cell.foodimg.image = imagereturn(urrl: foodimage[indexPath.row])
cell.unitlbl.text = foodunit[indexPath.row]
cell.callbl.text = foodcal[indexPath.row]
cell.switchFood.tag = indexPath.row
cell.switchFood.isUserInteractionEnabled = false
print(check[indexPath.row]!)
// check[indexPath.row] = false
if check[indexPath.row]!
{
cell.switchFood.isOn = true
}
else
{
cell.switchFood.isOn = false
}
cell.switchFood.addTarget(self, action: #selector(buttonClicked(Sender:)), for: UIControlEvents.touchUpInside)
return cell
}
保存按钮代码
func buttonClicked(Sender:UIButton)
{
print(Sender.tag)
let index = NSIndexPath(row: Sender.tag,section:0)
let cell = foodnametableview.cellForRow(at:index as IndexPath) as! AddDiseaseTableViewCell
if check[Sender.tag]!
{
// cell.switchFood.isUserInteractionEnabled = true
cell.switchFood.isOn = false
check[Sender.tag] = false
}
else
{
// cell.switchFood.isUserInteractionEnabled = true
cell.switchFood.isOn = true
check[Sender.tag] = true
}
}
最佳答案
您需要为每个类别创建不同的数组。而且你必须这样做:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if selectedCategory == .meat {
return meatArray.count
}
// same for other Catgories
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Create cell and other configurations
if selectedCategory == .meat {
let selectedFood = meatArray(indexPath.row)
//use selectedFood object to get values and use it in the cell
}
return cell!
}
关于ios - 如何在Pickerview中保存不同类别的tableviewCell的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951051/
我在一个 View Controller 上使用多个pickerview。我在相应的选取器 View 中获得正确的数据,但选取器 View 中的行对称 View 不正确。第一个选择器 View 中的数
我在 View 中有多个文本字段,其中输入 View 设置为 pickerView,但是根据文本字段,应该显示不同数量的选择行。当您单击选取器 View 外部,然后单击下一个文本字段时,此方法工作正常
我有两个 pickerView,即 pickerView1 = car_make、pickerView2 = car_model。 如果我选择 car_make = Kia,我希望 pickerVie
我创建了一个使用数组作为数据源的 UIPickerView,我想在第三个组件上选择一个项目后隐藏第四个组件上的一些项目。我该怎么做? class ViewControllerEspessuras: U
我尝试使用此方法selectedRow(inComponent: 0),但它对我不起作用。当选择器不活动时,有什么方法可以从选择器中获取第一个/默认值吗? func pickerView(_ pick
我一直在寻找答案,但找不到任何明确的解决方案。我正在制作一个具有三个具有相同 UIpickerViews 的 UItextFields 的 View Controller 。如果已在另一个 UItex
UIPickerView 显示数据,但不会传输到 UITextField。 我检查了其他帖子来比较我的代码,仍然找不到解决方案。非常感谢您的帮助。 import UIKit class ViewCon
当我尝试对 pickerView 中的特定行执行操作时,我遇到了一个问题。 例如,我在 pickerView 中有多个项目,而在核心数据中我有计数器属性,所以我需要做的是,一旦我从 pickerVie
我在现有应用程序中有一个使用小数位的自定义键盘。我正在尝试实现英制测量。 所以我从键盘上删除了小数点,并添加了一个选择器 View ,我希望在其中包含 1/32 -> 31/32 之间的所有可能的分数
当我点击文本字段时,会出现输入区域,但只有 1 行,内容为空白。 (我期望 3 行:a、b 和 c) 我将 numberOfRowsInComponent 和 titleForRow 函数中的返回值更
我正在尝试制作一个 pickerView,用户可以在其中选择他/她的体重...在 Swift 2.2 中,我正在考虑制作这样的 Range: var weightList: [Int] = Array
我是 iOS 新手。谁能帮助我如何使用 PickerView 来显示年份和月份?如何设置每列的大小? - (NSInteger)numberOfComponentsInPickerView:(UIPi
我使用 inputView 在单击文本字段时显示 pickerView。然后我想用数字 1-100 填充 pickerview。我有一些我认为可以工作的代码,但是当我运行程序并单击文本字段时,它只会弹
我制作了一个字体选择器,可以显示轮子内的所有字体。 我现在需要添加 2 个额外的部分来更改字体大小和样式(粗体、斜体、下划线)。 样式和大小是 UIFont 类的成员吗?如果是这样,如何访问它们? 这
这是我的代码: @synthesize pickerLetter, pickerNumber, pickerSymbol; - (void)viewDidLoad { [super viewDidL
我想要做的是根据从选择器中选择的内容显示特定的文本字符串。例如,可以在选择器中选择蓝色、绿色和黄色选项。当您选择蓝色时,一些文本(例如,我喜欢海洋!)会输出到标签。 我已经一切正常,除了在选择器中选择
我试图调出一个选择器 View 而不是键盘来放入文本字段,但我的选择器 View 没有显示。 #import "SettingsViewController.h" #import "FindCla
我正在从一个 JSON 文件中解析一些数据 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"airports" ofType
我正在尝试获取其中包含数字列表的选择器 View 。 到目前为止,我可以获得包含所有数字的字符串,但是当将其放入选择器 View 时,选择器 View 始终为空。你能看看我的代码,让我知道哪里出错了吗
我使用了一个带有四个组件的 UIPickerView 来在没有键盘的情况下输入数字。我添加了一个小数点,只有一个“。”在 UILabelView 中。查看屏幕了解想法。 我的 UIPickerView
我是一名优秀的程序员,十分优秀!