【问题标题】:how do I send selected uitableview rows to a newly created group如何将选定的 uitableview 行发送到新创建的组
【发布时间】:2017-06-28 00:31:08
【问题描述】:

我找不到这方面的任何信息。我想将我选择的行(带有复选标记的行)作为新创建的组发送到我的 firebase 数据库中。我在表格视图之外创建了一个“创建组”按钮。我很想获得有关如何从选定行传递数据的更多信息或资源,但我无法找到有关该主题的任何内容。我看到很多关于如何选择和取消选择带有复选标记的行的信息,但在我的情况下,没有将信息从选定的复选标记行发送到数组或在线数据库。

class FriendsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

 var userList = [Users]()

@IBOutlet weak var myTableView: UITableView!

@IBAction func createGroup(_ sender: Any) {
}
final let urlString = "https://api.lookfwd.io/v1/test/users"

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return userList.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
    myCell.selectionStyle = UITableViewCellSelectionStyle.none
    myCell.nameLabel.text = userList[indexPath.row].name
    return myCell
}



override func viewDidLoad() {
    super.viewDidLoad()

    self.downloadJsonWithTask()

}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if myTableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{

        myTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none}
    else{
        myTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
    }

            }



func downloadJsonWithTask() {

    let url = NSURL(string: urlString)

    var downloadTask = URLRequest(url: (url as URL?)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)

    downloadTask.httpMethod = "GET"

    URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in

        if let response = data {
            if let jsonData = try? JSONSerialization.jsonObject(with: response, options: .allowFragments) as? [String:Any] {

                if let dataArray = (jsonData as AnyObject).value(forKey: "users") as? [[String:Any]] {
                    for data in dataArray{
                        let newUser = Users(data: data)
                        self.userList.append(newUser)
                        print(jsonData!)
                    }
                }
                OperationQueue.main.addOperation({
                    for use in self.userList {
                        print(use.name ?? "")
                    }
                    self.myTableView.reloadData()

                })

                print(jsonData!)
            }
        }
    }).resume()
}

}

【问题讨论】:

    标签: ios swift uitableview didselectrowatindexpath


    【解决方案1】:

    您的用户对象存储在数组userList 中。您只需要像这样记录点击的索引

    var selected = [String]()
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if myTableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{
            myTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
            let currentUser = userList[indexPath.row]
            selected = selected.filter { $0 != currentUser.name}
        }else{
            myTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
            let currentUser = userList[indexPath.row]
            selected.append(currentUser.name)
        }
    }
    

    之后,当每一个你想得到所有选定的名字时

    for username in self.selected{
        print(username ?? "")
    }
    

    【讨论】:

    • 您能否详细说明如何通过所选数组的索引从 userList 中获取我的 name 元素?
    • @user8000557 循环遍历selected数组,通过索引从userList数组中获取User对象,然后从每个User中获取名称``
    • @IBAction func createGroup(_ sender: Any) { for (selected, name) in userList.enumerated(){ print(selected, name) } }
    • @user8000557 您只需要添加名称吗?还是所有用户对象?
    • 我只需要名称,我只需要将名称添加到我的 firebase 数据库中的组中
    猜你喜欢
    • 2017-06-22
    • 2021-01-20
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 1970-01-01
    • 2020-03-05
    • 2015-12-23
    相关资源
    最近更新 更多