我正在尝试将已检查的行从我的表视图发送到新创建的组到我的firebase数据库,一旦用户选择行并显示复选标记,我该如何将所选行信息发送到我的数据库。我不确定我是否应该在didSelectRowAt函数中执行此操作,或者在选择行后添加“创建组”按钮。任何信息都会有所帮助,谢谢。
import UIKit
class FriendsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var userList = [Users]()
@IBOutlet weak var myTableView: UITableView!
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()
}
}
答案 0 :(得分:0)
实现此目的的最佳方法是添加按钮而不是复选框,为所选和默认状态按钮设置复选框的图像,并在" cellForRowAt"方法这样做:
在你的" cellForRowAt"方法:
let button = cell.viewWithTag(123) as? UIButton // 123 is tag that is defined in Stoyboard for this button/checkbox.
button.accessibilityHint = "\(indexPath.row)"
button.addTarget(self, action: "action:", forControlEvents:
UIControlEvents.TouchUpInside)
将此方法粘贴到ViewController中的任何位置:
func action(sender:UIButton!) {
let position = Int(sender.accessibilityHint) // This is the position that will help you to get the specific item from your list.
if (sender.selected == true)
{
sender.setBackgroundImage(UIImage(named: "box"), forState:
UIControlState.Normal)
sender.selected = false
// Remove from Datbase
}
else
{
sender.setBackgroundImage(UIImage(named: "checkBox"), forState:
UIControlState.Normal)
sender.selected = true
// Add into database.
}
}
这是一个从Storyboard直接连接到ViewController的按钮的方法。对于您的表格视图,您可以按照以下步骤操作:
但是,如果您不想完成所有这些过程,那么只需在您的" didSelectRowAt"中添加数据库中的项目。方法,但它会在用户点击整个单元格的任何地方执行操作。