【问题标题】:Get JSON url from selecting an image or button to string in Swift 4在 Swift 4 中通过选择图像或按钮来获取 JSON url
【发布时间】:2017-12-29 10:15:10
【问题描述】:

你好 Stack Overflow 社区。​​p>

我目前正在处理我的新应用程序的界面。 当我尝试从 Web 服务器获取一些 JSON 数据时,我在将其实施到应用程序时遇到了一个小问题(不知道该怎么办)。

Basically what I'm trying to get is an Button or Image when selected gets me a selection in my JSON Url

以水果为例:

  1. 我选择了香蕉,会得到这个 JSON url:

    "https://www.examplewithfruits.com/getsomeFruit"+"banana"+"fromList"

  2. 我选择了苹果,会得到这个 JSON url:

    "https://www.examplewithfruits.com/getsomeFruit"+"apple"+"fromList"

重要的是,用户不需要在框中写下单词,而是选择图像或按钮。选择一些图像后,将在新页面上加载一个新活动。也可以将图像和文本框结合起来吗?(例如写下用户名)

对不起,如果这个问题很愚蠢,但我真的不知道解决这个问题的最佳方法是什么。希望提供一些示例或链接。 谢谢!

【问题讨论】:

  • 你正在使用 UItableView didSelectRow 方法,选择香蕉行你会得到 JSON url
  • 您可以使用 didSelectRow 方法,然后确定您刚刚选择了哪一行,然后选择保存在 var 或数组中的 2 个字符串之一来显示它们,以确定您选择的行检查它的行号例如如果使用选择香蕉和香蕉是第二个元素,那么行号应该是...等等尝试调试它以了解更多关于 (index.row)

标签: json iphone swift parsing swift4


【解决方案1】:
class ViewController: UITableViewDelegate,UITableViewDataSource{

    var imageArray =["Banana","Apple"]
    override func viewDidLoad() {

        super.viewDidLoad()
        // Do any additioa lf
        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell
        cell.imageView?.image = UIImage(named: imageArray[indexPath.row])
        return Cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // Your Json url

        var url = "https://www.examplewithfruits.com/getsomeFruit"+"\(self.imageArray[indexpath.row])"+"fromList"
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2023-03-22
    相关资源
    最近更新 更多