静态单元格上的复选标记 uitableview [英] Checkmark on static cells uitableview

查看:24
本文介绍了静态单元格上的复选标记 uitableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 UITableView,有 3 个部分(静态单元格)

I'm using a UITableView, with 3 sections ( Static Cells )

  • 货币
  • 语言
  • 社交

它们有不同的行数:

  • 货币有 3 行(美元、欧元、日元)
  • 语言有 2 行(EN、JP)
  • Social 有 3 行(Twitter、FB、Line)

现在,我默认在每个部分的第一行设置了一个复选标记.但是,我希望允许用户设置他们的默认设置并根据他们的设置相应地更改复选标记.

Right now, I have by default set a checkmark at the first row of every section. However, I would like to allow the user to set their default settings and change the checkmark accordingly based on what they have set.

我的问题是如何为 3 个不同的部分设置复选标记,每个部分都有不同的行数?

My question is then how do I set the checkmark for 3 different sections each with varying number of rows?

我需要为每个部分设置一个单元格标识符吗?我是否还需要为每个部分创建一个 UITableViewCell swift 文件?

Do I need to set an cell identifier for each Section? Do I also need to create a UITableViewCell swift file for each Section?

推荐答案

如果勾选了响应点击单元格,只需实现 tableView(_:didSelectRowAtIndexPath:):

If the checkmarks are set in response to tapping on the cell, just implement tableView(_:didSelectRowAtIndexPath:):

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
        }
    }
    // ... update the model ...
}

否则,您可以为情节提要中的每个单元格设置标识符(如果您愿意,也可以设置插座,因为这些单元格不会重复使用),然后只需以编程方式设置复选标记.例如,使用委托方法:

Otherwise, you can set identifiers for each cell in your storyboard (or outlets if you prefer, since the cells aren't reused), and then just set the checkmark programmatically. For example, using a delegate method:

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if let identifier = cell.reuseIdentifier {
        switch identifier {
            "USD Cell": cell.accessoryType = model.usdChecked ? .Checkmark : .None
            "EUR Cell": cell.accessoryType = model.eurChecked ? .Checkmark : .None
            //...
            default: break
        }
    }
}

不需要为每个部分/单元格创建单独的子类.

There shouldn't be a need to create a separate subclass for each section/cell.

这篇关于静态单元格上的复选标记 uitableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆