- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这很奇怪:我刚刚设置了一个新的单 View iOS 项目并将一个 TableView 放入 Main.storyboard 中。在这个 TableView 中,我放了一个 TableViewCell,在这个 Cell 中,我放了一个 UILabel 和一个 UISwitch。对于这个 TableViewCell,我创建了一个 CocoaTouchClass MyTableViewCell,并在 Interfacebuilder 中为 TableViewCell 的类设置了它。我将 UILabel 和 UISwitch 的 Outlets 连接到 MyTableViewCell,以及一个开关 Action 。我还连接了 TableView 的数据源并委托(delegate)给 ViewController。
所以,正如我所想,摆 table 的基本东西。
我的 ViewController 看起来像这样:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableData = [[String: Bool]]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
for index in 1...40 {
self.tableData.append([String(index): index%2 == 0])
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! MyTableViewCell
let object = tableData[indexPath.row].first!
cell.myLabel.text = object.key
cell.mySwitch.setOn(object.value, animated: false)
return cell
}
因此,我用一些数据行填充表格,并每隔一秒将 UISwitch 切换为打开状态。
MyTableViewCell-Class 也没什么特别的:
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var mySwitch: UISwitch!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
@IBAction func switched(_ sender: UISwitch) {
print("Switched: \(sender.isOn)")
}
好的,启动 iOS 模拟器,我看到了预期的表格。 40 条表格行无法显示在一个屏幕上,因此 TableView 使其自身可滚动。
现在:当我更改一个 UISwitch 的状态并拖动 TableView 以使更改后的 UISwitch 离开 View ,然后拖动 TableView 以使更改后的 UISwitch 再次可见时,它会变回其初始状态。Switch 事件被触发。
那么,我做错了什么?我错过了什么吗?
我录制了一个 4 秒的截屏视频来演示发生了什么: http://b-bereich.de/download/swiftSwitch.mov
最佳答案
TableViewCells 被重用。
这意味着您需要跟踪用于填充单元格内容的数据。如果单元格内容发生变化 - 例如当您点击单元格中的开关时 - 您需要更新您的数据源。当您滚动并再次显示该行时,您的数据将知道如何设置 Switch 的状态。
这是一个简单的例子:
//
// TableWithSwitchTableViewController.swift
// SWTemp2
//
// Created by Don Mag on 6/5/17.
// Copyright © 2017 DonMag. All rights reserved.
//
import UIKit
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var mySwitch: UISwitch!
var switchTapAction : ((Bool)->Void)?
@IBAction func switched(_ sender: UISwitch) {
print("Switched: \(sender.isOn)")
// send the Switch state in a "call back" to the view controller
switchTapAction?(sender.isOn)
}
}
// simple data object
class MyObject: NSObject {
var theTitle = ""
var theSwitchState = false
init(_ title: String) {
theTitle = title
}
}
class TableWithSwitchTableViewController: UITableViewController {
// array of MyObjects
var myData = [MyObject]()
override func viewDidLoad() {
super.viewDidLoad()
// just to make it a little easier to see the rows scroll
tableView.rowHeight = 60
// create 40 data objects for the table
for i in 1...40 {
let d = MyObject("Data Item: \(i)")
myData.append(d)
}
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SwitchCell", for: indexPath) as! MyTableViewCell
// Configure the cell...
let d = myData[indexPath.row]
cell.myLabel.text = d.theTitle
cell.mySwitch.isOn = d.theSwitchState
// set a "Callback Closure" in the cell
cell.switchTapAction = {
(isOn) in
// update our Data Array to the new state of the switch in the cell
self.myData[indexPath.row].theSwitchState = isOn
}
return cell
}
}
关于ios - TableViewCell 中的 Swift 3 UISwitch 在滚动时丢失状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44369289/
[将数据从一个 TableViewCell 传输到另一个 TableViewCell][1] 我有 CountriesViewController 和 tableView ,其中是不同国家/地区的列表
我的 TableView 里面有 3 个 TableViewCell。 第一个 TableViewCell 有:标签 第二个 TableViewCell 有:TextField 第三个 TableVi
我不确定这是否是一个奇怪的设置,但我有两个自定义 tableView 单元格 - 手机手机 电子邮箱 在 PhoneCell 中,我有两个文本字段,这些文本字段中有一些值。在 EmailCell 中,
所以我有一个 ExpyTableView 类型的 mainTableView ( https://github.com/okhanokbay/ExpyTableView )。一切都很好,我设法实现了它
据我了解,我无法使用 socket 连接将单元格中的按钮连接到我的 TableViewController。 不过,我可以使用操作连接将单元格中的按钮连接到我的 TableView。这是我巨大问题的根
我想在动态 UITableViewCell 中添加一个动态 UICollectionView,当 CollectionView 接收并准备 contentView 时,它会自动更新其高度。有什么好的方
I want different images under different heading //ViewController.swift var categories = ["Good Aft
我有以下表格 View : 当用户点击蓝色的 Pay Cash/Card 按钮时,他们会转到另一个 tableView。 我遇到的问题是;当用户单击按钮时,不会在委托(delegate)方法中捕获 t
我有一个带有自定义单元格的 TableView。单元格中的 subview 之一是 UIButton。当用户单击按钮时,我希望背景发生变化。我得到了所有的工作。但问题是,直到我将受影响的单元格滚动出屏
- (void)viewDidLoad { [super viewDidLoad]; [self.tableView registerNib:[UINib nibWithNibName
我今天花了六个多小时研究并尝试创建动态单元,但没有成功。我也找到了一些很棒的教程。我做错了什么,希望有人能告诉我什么。 我的应用: table 开始是空的。单击添加按钮会显示一个模态 VC,其中有两个
请注意此代码。 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITa
我有一个 tableViewCell,总共包含 8 个图像,分为两个 block (每个 block 4 个图像)。这些图像会异步下载并存储到数组中,然后在 tableViewCell 的 cellF
运行我的弹出窗口时,代码显示在左上角,而不是显示在单击图标的同一位置,是否可以更改某些内容来纠正位置? 最佳答案 这是因为您已将 aView 的 CGRect 设置为 x 和 y 为 0。 如果您以编
我用 CustomTableViewCells 制作了一个应用程序。我在 MainStoryBoard 上设置了一个 UIButton。 我想在点击时更改按钮的图像。当我再次点击时,我希望按钮返回到原
我正在编写一个 iOS 应用程序。用户输入电影搜索关键字,它会从 omdbapi.com 获取数据并在表格 View 中显示电影。问题是该 TableView 单元格未正确显示。 项目网址: proj
我目前有一个表格 View ,其中加载的单元格中有一个 uicollectionview。我目前遇到了表格 View 单元格的两个问题。 1) TableView 中的第一个单元格应该具有紫色背景,但
我有一个 TableView,可以在其中加载自定义单元格。在自定义单元格中,有一个文本字段,当表格 View 填充自身时,该文本字段会加载自定义拨号盘并自行关闭。这个 float 数字键盘在初始化期间
这是我的主屏幕当单击单元格突出显示时,描述将转到下一个 View Controller ,并且单元格会突出显示,当我单击未突出显示的单元格时,因此单元格未突出显示,这意味着只有零索引单元格突出显示,没
我想以编程方式将两个文本字段添加到 tableViewCell 子类(稍后会详细介绍),但很难从 View Controller 访问文本字段。 我的 TableView 子类是: - (id)ini
我是一名优秀的程序员,十分优秀!