UITableViewCell 复选标记在点击时打开和关闭

2024-03-25

我正在处理表格视图

我希望能够点击每个单元格,点击时,它会在单元格上显示一个复选标记

现在我有一些代码可以实现此功能:

// checkmarks when tapped

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let section = indexPath.section
    let numberOfRows = tableView.numberOfRowsInSection(section)
    for row in 0..<numberOfRows {
        if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) {
            cell.accessoryType = row == indexPath.row ? .Checkmark : .None
        }
    }
}

但此代码仅选择一个部分内的 1 个单元格(我有 5 个部分)

我需要它来选择任何地方的任何单元格

另外,当我上下拖动屏幕时,我会被复选标记丢失

视图控制器.swift

class ViewController: UIViewController, UITableViewDataSource {                        //class and subclass                  |)
//---------------------------------------------------------------------------------------------------------------------------/
    // Variable and constant, also IBAOutlet

    let section1 =
       ["this is used",
        "this is used to test",
        "this is used to test the lenght",
        "this is used to test the lenght of the text",
        "this is used to test the lenght of the text",
        "this is used to test the lenght of the text",
        "this is used to test the lenght of the text",
        "this is used to test the lenght of the text",
        "this is used to test the lenght of the text",]
    let section2 =
       ["this is used to test the lenght of the text"]
    let section3 =
       ["this is",
        "this is ",]


    @IBOutlet weak var scoreshow: UILabel!
    @IBOutlet weak var reset: UIButton!
    @IBOutlet weak var tableView: UITableView!

// --------------------------------------------------------------------------------------

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()
    }
//----------------------------------------------------------------------------------------
    // checkmarks when tapped

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            if cell.accessoryType == .Checkmark
            {
                cell.accessoryType = .None
            }
            else
            {
                cell.accessoryType = .Checkmark
            }
        }    
    }
//----------------------------------------------------------------------------------------
    //number of sections for the table

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 5
    }
//----------------------------------------------------------------------------------------
    //Calculate the amount of rows

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return self.section1.count;
    }
//----------------------------------------------------------------------------------------
    //Cells text label and config

    func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
        cell.textLabel!.text = section1[indexPath.row]
        cell.textLabel!.numberOfLines = 0

        return cell
    }

//----------------------------------------------------------------------------------------

    @IBAction func resetswitch(sender: UIButton) {




    }
//----------------------------------------------------------------------------------------

}

斯威夫特 > 3.0

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .none
    }
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.accessoryType = .checkmark

    }
}

我通过使用两个 Swift 函数来解决:didSelectRowAtIndexPath 和 didDeselectRowAtIndexPath。

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.accessoryType = .None
    }
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {
        cell.accessoryType = .Checkmark

    }
}

为了使其正常工作,请在 cellForRowAtIndexPath 函数中添加一行代码,以便在屏幕上绘制表视图时选择一行,否则在您第一次选择另一行时将不会调用 didDeselectRowAtIndexPath 。就像这样:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellData", forIndexPath: indexPath) 
    if (some condition to initially checkmark a row)
        cell.accessoryType = .Checkmark
        tableView.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.Bottom)
    } else {
        cell.accessoryType = .None
    }

    return cell
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITableViewCell 复选标记在点击时打开和关闭 的相关文章

  • 将文件保存到 icloud 驱动器

    我正在尝试将文件保存到 icloud 驱动器 我要使用简单版本 不让用户选择保存文件的位置 我只是将其保存在 icloud 驱动器的根目录中 这是我正在使用的代码 func exportToFiles for page in pages l
  • 如何在 iPhone 应用程序中播放来自服务器的视频 URL

    实际上 我通过使用从服务器获取一个网址XMLParser 我想在我的应用程序中播放这个视频网址 现在我使用了 MPMoviePlayerController 但它不起作用 请帮忙 XML解析器 m id loadXMLByURL NSStr
  • Apple 如何通知 iOS 应用程序内购买 (IAP) 退款?

    我在我的应用程序中成功实现了 Apple iOS IAP 并在沙箱中进行了测试 效果很好 我担心用户可能会通过 IAP 购买商品 将其下载到我的应用程序中 然后向 Apple 投诉并获得退款 没有明显的方式将退款报告给我的应用程序 它们是否
  • 如何在 Swift 中检查 while 循环条件中的“nil”?

    如何检查nil在 Swift 的 while 循环中 我在这方面遇到错误 var count UInt 0 var view UIView self while view superview nil Cannot invoke with a
  • 枚举 NSString 的最佳方法

    我正在寻找枚举 objc 对象 例如 NSString 的方法 我记得 Xcode4 版本中有一个新功能 它提供了一种新的 enum 方法 但不清楚 有人知道吗 好吧 我自己回答 我想我犯了一个错误 这就是我上面提到的新功能 typedef
  • Swift 4 使用随机密钥解码嵌套 JSON [重复]

    这个问题在这里已经有答案了 我是 Swift 4 的新手 正在尝试从 Wikipedia API 解码此 JSON 我正在努力定义一个结构 因为我发现的所有示例 教程都仅嵌套 1 2 层深度 除此之外 当其中一个密钥是随机的时 如何解码数据
  • 为什么 UIWebView 实例不调用scrollViewDidScroll?

    iOS 文档说 UIWeb视图 http developer apple com library ios documentation uikit reference UIWebView Class Reference Reference h
  • UITableView 如何一次显示两个不同的数组?

    下面的代码有效 但不符合我的意愿 我希望当我单击 UIbutton 时 它会自动更新 UITableview 中的新值而不是旧值 下面的代码仅在我按下 UIbuttons 时有效 之后当我滚动 UITableview 时它会更新具有新值的
  • 关于 S3 文件传输的权限

    我正在使用 S3TransferManager Sample 进行测试 我创建了Cognito并设置了IAM并最后更改了constants swift文件 我上传没有问题 但下载失败 错误信息是 下载失败 错误域 com amazonaws
  • 将 firebase 框架添加到 ios 项目时出现链接器错误

    我一直在尝试将 firebase 框架添加到我的 ios 应用程序中 我按照这里的说明进行操作 https www firebase com docs ios quickstart html https www firebase com d
  • 禁用将应用程序窗口置于前面。关闭另一个窗口后

    我有包含 2 个 NSWindowController 的 OSX 应用程序 我的问题可以通过以下几个步骤来描述 使用 2 个窗口启动应用程序 选择另一个应用程序的窗口 将其中一个窗口放在另一个应用程序窗口前面 第二个窗口将保留在底部 关闭
  • 如何防止 iPhone 4 在添加到主屏幕时截断我的标题?

    我的网页标题很长 当添加到 iPhone 旧版本的主屏幕时 整个标题在确认对话框中可见 当我添加到 iPhone 4 的主屏幕时 标题在第 12 个符号之后被切断 有没有办法阻止这种剪切 让 iPhone 默认显示完整标题 显然会有一个无证
  • Xcode 6 代码编辑器异常行为

    截至最近 我在使用 Xcode 6 代码编辑器时遇到了一些麻烦 最终导致编辑窗口中的焦点快速变化 在编辑文本时跳转到同一文件中完全不同的位置 一切就是这样开始的 最近 编辑器拒绝上下滚动 挂起 停止显示行号或任何其他文本 我该怎么做才能让它
  • iOS:如何在 UITabBarItem 中添加下划线

    我正在使用一个应用程序 我需要在其中添加下划线UITabbarItem 所以我想在所选下添加下划线UITabbarItem在默认情况下UITabbarcontroller of iOS 我已经创建了子类UITabbarcontroller但
  • 在React-native中,如何更改NavigatorIOS的样式

    在react native中 如何更改NavigatorIOS的样式 例如背景颜色 谢谢你 var speedNews React createClass render function return
  • CF 类型的带有 __attribute__((NSObject)) 的强 @property 不会保留

    更新 自 Xcode 4 6 起 此问题已得到修复 现在 这项技术再次按预期发挥作用 但是 在代码中使用之前 请务必阅读 Rob Napier 出色答案顶部的注释 原帖 ARC Xcode 4 3 1 iOS 5 1 我有一个 CF 类型
  • 用于 Flutter 原生广告的 Objective-C 的 Swift 等效项

    我想为我的 Flutter 项目实现原生广告 它使用 Swift 而不是 Objective C https developers google com admob flutter native https developers googl
  • VNFaceObservation BoundingBox 在纵向模式下不缩放

    作为参考 这源于一个问题视觉API 我正在努力使用Vision通过a检测图像中的人脸VNDetectFaceRectanglesRequest 它在确定图像中正确的人脸数量并提供boundingBox对于每张脸 我的麻烦是由于我UIImag
  • 去除iOS输入阴影

    在 iOS Safari 5 上 我必须遵循输入元素 顶部内部阴影 我想删除顶部阴影 错误 webkit appearance不保存 目前的风格是 input border radius 15px border 1px dashed BBB
  • UITableView 自动调整行大小约束在 iPhone 6Plus 上神秘破坏

    我有一个自定义的 UITableViewCell 它有一个缩略图和一堆文本 行高配置为使用自动计算 tableView estimatedRowHeight 129 tableView rowHeight UITableViewAutoma

随机推荐