首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多选tableView

多选tableView
EN

Stack Overflow用户
提问于 2017-07-07 06:25:15
回答 2查看 2.1K关注 0票数 0

我的应用程序的某个部分我希望用户能够选择一个表中的位置,如果他们想要多个,并有这些位置打印到标签,以便他们可以看到他们选择了整齐和前面。要执行此操作,我需要在didSelectRowAt indexPath中输入什么内容?它们都在同一个ViewController上。

当我选择一个表单元格时,我不想转到另一个视图控制器。

代码语言:javascript
复制
@IBOutlet weak var labelGifteeLocationsPreview: UILabel!
@IBOutlet weak var tableLocations: UITableView!
let itemsLocations: [String] = ["Pacific Northwest", "West Coast", "The West", "Midwest", "Great Lakes", "The South", "New England"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return itemsLocations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellLocations: UITableViewCell = UITableViewCell()
    cellLocations.textLabel?.text = itemsLocations[indexPath.row]
    return cellLocations
}

编辑

现在,每当我选择一个离表更低的项目时,都会带来索引超出范围的错误。我不知道为什么只有当我选择离表更远的项目时才会发生这种情况。代码如下:

代码语言:javascript
复制
@IBOutlet weak var tableLocations: UITableView!
let itemsLocations: [String] = ["Pacific Northwest", "West Coast", "The West", "Midwest", "Great Lakes", "The South", "New England"]
@IBOutlet weak var labelGifteeLocationsPreview: UILabel!
var locationsPreviewtext = [String]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    locationsPreviewtext.append(itemsLocations[indexPath.row])
    labelGifteeLocationsPreview.text = "\(locationsPreviewtext)"
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if locationsPreviewtext.isEmpty {
        return
    }
    else {
        let ind = index(ofAccessibilityElement: locationsPreviewtext[indexPath.row])   //this is where I get the index out of range error
        if ind == NSNotFound {
            locationsPreviewtext.remove(at: indexPath.row)
        }
    }
    labelGifteeLocationsPreview.text = "\(locationsPreviewtext)"
EN

回答 2

Stack Overflow用户

发布于 2017-07-07 14:43:58

代码语言:javascript
复制
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath){

        let cell = tableLocations.cellForRowAtIndexPath(indexPath)

        if (cell?.accessoryType == UITableViewCellAccessoryType.Checkmark){

            cell!.accessoryType = UITableViewCellAccessoryType.None;

        }else{

            cell!.accessoryType = UITableViewCellAccessoryType.Checkmark;

        }
    }
票数 0
EN

Stack Overflow用户

发布于 2017-11-26 17:14:59

Swift 4

代码语言:javascript
复制
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

    let cell = tableView.cellForRow(at: indexPath)

    if (cell?.accessoryType == UITableViewCellAccessoryType.checkmark){

        cell!.accessoryType = UITableViewCellAccessoryType.none

    }else{

        cell!.accessoryType = UITableViewCellAccessoryType.checkmark

    }
}

另一种方式,我推荐它

代码语言:javascript
复制
var tags = [["id":1,"name":"html"],["id":2,"name":"php"],["id":3,"name":"javascript"],["id":4,"name":"python"]]
var selectedTags = [Int]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tags.count
    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = Bundle.main.loadNibNamed("TagsTableViewCell", owner: self, options: nil)?.first as! TagsTableViewCell

    if(selectedTags.contains(tags[indexPath.row]["id"].intValue)){
        cell.accessoryType = .checkmark
    }
    else{
        cell.accessoryType = .none
    }
    return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

    if(selectedTags.contains(tags[indexPath.row]["id"].intValue)){
        selectedTags = selectedTags.filter { $0 != tags[indexPath.row]["id"].intValue }
    }
    else{
        selectedTags.append(tags[indexPath.row]["id"].intValue)
    }

    tagsTableView.reloadData()

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44959809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档