【问题标题】:Parsing JSON Tableview Information as Separate Strings将 JSON Tableview 信息解析为单独的字符串
【发布时间】:2017-04-17 22:52:42
【问题描述】:

我目前正在尝试使用已填充 JSON 的 Tableview 单元格中的信息来执行各种操作,但由于它不在单个字符串中,因此我无法调用特定信息。有什么方法可以将我拉入每个 tableview 单元格的数据组并将其转换为一系列单独的字符串?这是我目前拥有的:

import UIKit
import GoogleMobileAds

class OngoingViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var userUsernameLabel: UILabel!
    @IBOutlet weak var bannerView: GADBannerView!

    @IBOutlet weak var ongoingTable: UITableView!

    var list:[MyStruct] = [MyStruct]()

    struct MyStruct
    {
        var user1 = ""
        var user2 = ""
        var wager = ""
        var amount = ""

        init(_ user1:String, _ user2:String, _ wager:String, _ amount:String)
        {
            self.user1 = user1
            self.user2 = user2
            self.wager = wager
            self.amount = amount
        }
    }

    override func viewDidLoad() {

        super.viewDidLoad()

        let username = UserDefaults.standard.string(forKey: "userUsername")!
        userUsernameLabel.text = username


        /// The banner view.
        print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())
        bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
        bannerView.rootViewController = self
        bannerView.load(GADRequest())


        ongoingTable.dataSource = self
        ongoingTable.delegate = self

        get_data("http://cgi.soic.indiana.edu/~team10/ongoingWagers.php")
    }


    func get_data(_ link:String)
    {
        let url:URL = URL(string: link)!


        var request = URLRequest(url:url);
        request.httpMethod = "POST";


        let postString = "a=\(userUsernameLabel.text!)";


        request.httpBody = postString.data(using: String.Encoding.utf8);


        let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in



            self.extract_data(data)

        }

        task.resume()
    }


    func extract_data(_ data:Data?)
    {
        let json:Any?

        if(data == nil)
        {
            return
        }

        do{
            json = try JSONSerialization.jsonObject(with: data!, options: [])
        }
        catch
        {
            return
        }

        guard let data_array = json as? NSArray else
        {
            return
        }


        for i in 0 ..< data_array.count
        {
            if let data_object = data_array[i] as? NSDictionary
            {
                if let data_user1 = data_object["id"] as? String,
                    let data_user2 = data_object["id2"] as? String,
                    let data_wager = data_object["wager"] as? String,
                    let data_amount = data_object["amount"] as? String
                {
                    list.append(MyStruct(data_user1, data_user2, data_wager, data_amount))
                }

            }
        }


        refresh_now()


    }

    func refresh_now()
    {
        DispatchQueue.main.async(
            execute:
            {
                self.ongoingTable.reloadData()

        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

        return list.count
    }

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

        let cell = self.ongoingTable.dequeueReusableCell(withIdentifier: "owcell", for: indexPath) as! OngoingTableViewCell

        cell.infoLabel.text = list[indexPath.row].user1 + " " +  list[indexPath.row].user2 + " " + list[indexPath.row].wager + " " + list[indexPath.row].amount

        cell.user1Button.tag = indexPath.row
        cell.user1Button.addTarget(self, action: #selector(OngoingViewController.user1Action), for: .touchUpInside)

        cell.user2Button.tag = indexPath.row
        cell.user2Button.addTarget(self, action: #selector(OngoingViewController.user2Action), for: .touchUpInside)

        return cell

    }


    @IBAction func user1Action(sender: UIButton) {

        let user1Alert = UIAlertController(title: "Wait a second!", message: "Are you sure this user has won this wager?", preferredStyle: UIAlertControllerStyle.alert)
        user1Alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: { action in

            let user1ConfirmationAlert = UIAlertController(title: "Great!", message: "Please wait for the other user to confirm the winner of this wager.", preferredStyle: UIAlertControllerStyle.alert)
            user1ConfirmationAlert.addAction(UIAlertAction(title: "Got It!", style: UIAlertActionStyle.default, handler: nil))
            self.present(user1ConfirmationAlert, animated: true, completion: nil)

        }))
        user1Alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
        self.present(user1Alert, animated: true, completion: nil)

    }

    @IBAction func user2Action(sender: UIButton) {

        let user2Alert = UIAlertController(title: "Wait a second!", message: "Are you sure this user has won this wager?", preferredStyle: UIAlertControllerStyle.alert)
        user2Alert.addAction(UIAlertAction(title: "Yes", style: UIAlertActionStyle.default, handler: { action in

            let user2ConfirmationAlert = UIAlertController(title: "Great!", message: "Please wait for the other user to confirm the winner of this wager.", preferredStyle: UIAlertControllerStyle.alert)
            user2ConfirmationAlert.addAction(UIAlertAction(title: "Got It!", style: UIAlertActionStyle.default, handler: nil))
            self.present(user2ConfirmationAlert, animated: true, completion: nil)

        }))
        user2Alert.addAction(UIAlertAction(title: "No", style: UIAlertActionStyle.default, handler: nil))
        self.present(user2Alert, animated: true, completion: nil)

    }


}

这是 OngoingTableViewCell 子类:

import UIKit

class OngoingTableViewCell: UITableViewCell {

    @IBOutlet weak var infoLabel: UILabel!
    @IBOutlet weak var user1Button: UIButton!
    @IBOutlet weak var user2Button: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

【问题讨论】:

  • “一系列单独的字符串”是什么意思?
  • 所以这个 JSON 正在插入“user1, user2, beer, amount”。我希望能够将它们单独切碎,以便在将它们插入 tableview 单元格后对它们执行操作。如果这有意义...例如,我想在条件语句中包含“user1”,但我不能,因为它不是它自己的字符串。
  • 所以你想从user1Action方法中点击的行访问用户?
  • 我希望 user1Action 从 tableview 单元格中获取信息并将其与 userDefault 进行比较。
  • 我只需要找到一种方法将该用户与填充 tableview 单元格的 JSON 数组隔离

标签: ios json swift string uitableview


【解决方案1】:

您有一个 MyStruct 结构数组,其中包含用户 1、用户 1、赌注和金额的条目。这很好。

您在按钮上使用标签作为确定所选单元格的一种方式,这并不理想。相反,我建议使用 sender 参数来找出包含按钮的单元格的 indexPath。有关更好方法的详细信息,请参阅我的答案的底部。

在任何情况下,一旦你有了一个行号,你就可以通过索引到你的数组来轻松地获得该赌注的数据:

@IBAction func user1Action(sender: UIButton) {

  selectedRow = sender.tag
  //or, using my function:
  selectedRow = tableView.indexPathForView(sender)

  //Get the wager for the button the user tapped on.
  let thisWager = list[selectedRow]
}

如果您想在用户点击您的UIAlertController 中的按钮后对投注采取行动,请不要在您的第二个警报控制器上使用 nil 处理程序。相反,传入一个闭包,该闭包使用上面代码中的selectedRow 参数来索引投注列表,甚至使用我在代码中显示的thisWager 局部变量。


获取用户点击的按钮的 indexPath:


我为 UITableView 创建了一个简单的扩展,它允许您传入 UIView(就像按钮操作中的发送者)并取回包含该视图的 indexPath。

这个扩展非常简单。下面是它的外观:

public extension UITableView {
  
  /**
  This method returns the indexPath of the cell that contains the specified view
   - Parameter view: The view to find.
   - Returns: The indexPath of the cell containing the view, or nil if it can't be found
  */
  
  func indexPathForView(_ view: UIView) -> IndexPath? {
    let origin = view.bounds.origin
    let viewOrigin = self.convert(origin, from: view)
    let indexPath = self.indexPathForRow(at: viewOrigin)
    return indexPath
  }
}

您可以从按钮的操作中调用该函数:

@IBAction func buttonTapped(_ button: UIButton) {
  if let indexPath = self.tableView.indexPathForView(button) {
    print("Button tapped at indexPath \(indexPath)")
  }
  else {
    print("Button indexPath not found")
  }
}

整个项目可以在 Github 上的这个链接找到:TableViewExtension

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2015-04-04
    • 2015-11-23
    • 2016-07-02
    • 2019-05-17
    相关资源
    最近更新 更多