Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何存储选定行的数组?_Ios_Arrays_Swift_Uitableview - Fatal编程技术网

Ios 如何存储选定行的数组?

Ios 如何存储选定行的数组?,ios,arrays,swift,uitableview,Ios,Arrays,Swift,Uitableview,在我的代码中,我有一个表视图,用于加载使用我的应用程序的用户的名称。当用户选择一行时,它将显示一个复选标记。我要做的是保存一个包含所有选定行的数组(该数组将包含名称)。我找到了一些信息,但我还在学习iOS编程,我不知道Obj-c 这就是我到目前为止所做的: var selectedMembers = [String]? func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

在我的代码中,我有一个表视图,用于加载使用我的应用程序的用户的名称。当用户选择一行时,它将显示一个复选标记。我要做的是保存一个包含所有选定行的数组(该数组将包含名称)。我找到了一些信息,但我还在学习iOS编程,我不知道Obj-c

这就是我到目前为止所做的:

var selectedMembers = [String]?
 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.names?.count ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("UserCell") as! UITableViewCell

        cell.textLabel!.text = names![indexPath.row]

        return cell
    }


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    selectedMembers = Array()
    if let cell = tableView.cellForRowAtIndexPath(indexPath) {

        if cell.accessoryType == .Checkmark
        {
            cell.accessoryType = .None

            self.selectedMembers?.remove(indexPath)

        }
        else
    {
        cell.accessoryType = .Checkmark
        self.selectedMembers?.append(indexPath)
        }
    }
}

要获取名称,您需要使用所选行,并将该行的条目与名称一起放入数组中

要获取一行
indepath
,可以使用

indexPath.row
要获取您的成员名称,请使用

names![indexPath.row-1]
当然,您可以使用

self.selectedMembers?.append(names![indexPath.row-1])
要删除该项目,您需要添加额外的步骤

self.selectedMembers?.removeAtIndex(selectedMembers.indexOf(names![indexPath.row-1]))

您可以像这样使用didSelectRowAtIndexPath和didSelectRowAtIndexPath委托方法来跟踪表中的索引路径

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    selectedIndexPaths.append(indexPath)

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    if let index = find(selectedIndexPaths, indexPath) {
        selectedIndexPaths.removeAtIndex(index)
    }

}

然后,您可以使用索引路径回溯并获取所选对象。

可能重复的只是检查此问题,是关于如何将Swift数组保存到文件是您的问题如何保存数组以供以后使用或如何获取所选成员的名称(行中的字符串)@milo526我将使用这些名称(保存在数组中)以创建一个组。使用此解决方案,我收到消息:“致命错误:数组索引超出范围”。我删除了“-1”,在数组中只得到了最后一个选定成员的名称。能否指定返回错误的行?此行:self.selectedMembers?.append(名称![indexPath.row-1])。当我选择行时,应用程序崩溃。在您的界面生成器中,您是否启用了选择多行的可能性(您是否看到多个复选标记)。是的,复选框显示在所有选定的用户中。它会在selectedMembers.append(indexPath)行显示消息:“无法使用类型为“(NSIndexPath)”的参数列表调用“append”确保像这样初始化它
var selectedIndexPaths=[NSIndexPath]()
hey's find(selectedIndexPaths,indexPath)?我使用未解决标识符“find”时出错。