【问题标题】:Swift TableView Reloads but Cells Remain Unchanged Until ClickedSwift TableView 重新加载,但单元格在单击之前保持不变
【发布时间】:2014-08-27 06:06:06
【问题描述】:

Swift 中,我使用Segmented ControllerJSON 填充我的TableView,这取决于选择的细分。两个JSON 文件都已成功加载,因为在我的第一个有两个单元格,而在我的第二个有3 个单元格。当我单击第二个段时,在当前查看的两个单元格下方,会出现第三个。问题是尽管我的self.tableView .reloadData() 在我的Segmented ControllerIBAction 和我的viewWillAppear 函数中都调用了,但它上面的两个不会重新加载。

单击单元格时,会显示相应的信息并且一切正常,但直到我单击该行时才会显示。什么给了?

我的 MainVC.swift 文件

class MainVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    var dataArray: NSArray = NSArray()

    @IBOutlet var Controller: UISegmentedControl!

    override func viewWillAppear(animated: Bool) {
        self.tableView .reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        startConnectionAt("http://www.domain.com")
        // Do any additional setup after loading the view.
    }

    @IBAction func Change(sender: AnyObject) {

        if Controller.selectedSegmentIndex == 0 {
            startConnectionAt("http://www.domain.com")
        }
        else if Controller.selectedSegmentIndex == 1 {
            startConnectionAt("http://www.domain.com")
        }

        self.tableView .reloadData()

    }

//MARK: JSON Loading

    var data: NSMutableData = NSMutableData()
    func startConnectionAt(urlPath: String){
        var url: NSURL = NSURL(string: urlPath)
        var request: NSURLRequest = NSURLRequest(URL: url)
        var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
        connection.start()
    }

    func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
        println("Connection failed.\(error.localizedDescription)")
    }

    func connection(connection: NSURLConnection, didRecieveResponse response: NSURLResponse)  {
        println("Recieved response")
    }

    func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        self.data = NSMutableData()
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
        self.data.appendData(data)
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        var dataAsString: NSString = NSString(data: self.data, encoding: NSUTF8StringEncoding)
        var err: NSError
        var json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
        var results: NSArray = json["needs"] as NSArray
        self.dataArray = results

        tableView.reloadData()
        println("success")
    }

//End loading of JSON

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

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count;
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomCell

        var rowData: NSDictionary = dataArray[indexPath.row] as NSDictionary
        var title=rowData["needFirstname"] as String
        var poster=rowData["needCountry"] as String
        var descrip=rowData["needDescription"] as String


        //GDC
        var queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

        dispatch_async(queue, {
            cell.needTitle.text = title
            cell.needPoster.text = poster
            cell.needDescription.text = descrip
            cell.needDescription.numberOfLines = 0
        })

        return cell
    }

}

我的 CustomClass.swift 文件(用于自定义 UITableViewCell)

class CustomCell: UITableViewCell {
    @IBOutlet var needTitle: UILabel!
    @IBOutlet var needPoster: UILabel!
    @IBOutlet var needDescription: UILabel!
}

【问题讨论】:

    标签: ios uitableview swift uisegmentedcontrol


    【解决方案1】:

    问题是您将单元格的 UI 元素设置在后台线程上,此处:

    //GDC
    var queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    
    dispatch_async(queue, {
        cell.needTitle.text = title
        cell.needPoster.text = poster
        cell.needDescription.text = descrip
        cell.needDescription.numberOfLines = 0
    })
    

    我不确定您为什么要使用 GCD 来调度那里的后台队列,但您不应该这样做。 所有 UI 元素必须始终从主线程/队列更新。

    只需摆脱您的dispatch_async,它应该可以工作:

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomCell
    
        var rowData: NSDictionary = dataArray[indexPath.row] as NSDictionary
        var title=rowData["needFirstname"] as String
        var poster=rowData["needCountry"] as String
        var descrip=rowData["needDescription"] as String
    
        cell.needTitle.text = title
        cell.needPoster.text = poster
        cell.needDescription.text = descrip
        cell.needDescription.numberOfLines = 0
    
        return cell
    }
    

    【讨论】:

    • 非常感谢!这就是我盲目遵循教程所得到的!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2020-03-17
    相关资源
    最近更新 更多