ios - 添加 JSON 数据时 UITableView 不工作

标签 ios json swift uitableview

我正在尝试获取一个 TableView 来加载从 imgur 获取的一些 JSON 数据。我可以向后端发出请求,后端获取 imgur 数据,将其返回给我,然后我可以填充表。然而,我遇到了一个非常奇怪的问题,数据最后被切断。 Here是一张图片。如果我尝试进行另一次搜索,它不会将更多数据加载到表中,所以很明显,当我这样做时,我会破坏它。我真的不知道我做错了什么。我还试图让它在某个时候可以通过模态点击,并且使可重用单元出队可能也不正确,但我想在这个问题之后我会到达那里。

无论如何,任何帮助将不胜感激。

class ViewController3: UIViewController, UITextFieldDelegate,UITableViewDataSource {

    var dataState: Data?

    var linkArray = [String]()
    var titleArray = [String]()

    @IBOutlet weak var tableView: UITableView!

    @IBOutlet weak var SearchTextField: UITextField!

    @IBAction func SearchButton(_ sender: Any) {

        let urlString = "http://localhost:3000/getimages"


        Alamofire.request(urlString, method: .post, parameters: ["search": SearchTextField.text!],encoding: JSONEncoding.default, headers: nil).responseJSON {
            response in
            switch response.result {
            case .success:
                let json = JSON(response.data!)


                for (_, subJson) in json["data"] {
                    if let link = subJson["link"].string {
                       self.linkArray.append(link)
                    }
                    if let title = subJson["title"].string{
                        self.titleArray.append(title)
                    }
                }
                print("this is linkArray ", self.linkArray)
                print("this is titleArray ", self.titleArray)
                self.dataUpdated()
                break
            case .failure(let error):
                print(error)
            }
        }


    }


    @IBAction func BackToTabControl(_ sender: Any) {
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let tabBarVC = storyBoard.instantiateViewController(withIdentifier: "tabBarController") as! UITabBarController

        if let vc1 = tabBarVC.viewControllers?.first as? ViewController1 {
            vc1.dataState = dataState
        }

        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = tabBarVC
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.bounces = true
        SearchTextField.returnKeyType = UIReturnKeyType.done
        SearchTextField.delegate = self
        tableView.dataSource = self

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! //1.

        let text = titleArray[indexPath.row] //2.

        cell.textLabel?.text = text //3.

        return cell //4.
    }

    func dataUpdated() {
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

最佳答案

下一步尝试:

    if let label = cell.textLabel {
        label.text = text
        label.sizeToFit()
    }

关于ios - 添加 JSON 数据时 UITableView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44766340/

相关文章:

json - 从 anyContent 获取内容为 Json

arrays - 将 array.count 转换为字符串

SWIFT 3 - 没有 '*' 候选人产生预期的上下文结果类型 'NSNumber'

iphone - 在应用商店提交应用时出现版本号问题

iphone - UIImagePickerController imagePicker 不适合 iPhone 4 屏幕

ios - 使用 GIDSignIn.sharedInstance.clientID = 时的错误代码

java - 使用 XStream 和 JsonHierarchicalStreamDriver 输出值,如何舍入 double ?

java - 使用 Retrofit 解析 Android 的 JSON

swift - 从 DateInterval 数组中排除 DateInterval 数组

ios - 上传多张图片到服务器 IOS