Swift TableView重新加载但单元格保持不变直到单击

时间:2014-08-27 06:06:07

标签: ios uitableview swift uisegmentedcontrol

Swift中,我使用Segmented Controller根据选择的细分来填充TableView JSON JSON。两个self.tableView .reloadData()文件都加载成功,因为在我的第一个文件中有两个单元格,在我的第二个文件中有3个。当我单击第二个段时,在当前查看的两个单元格下方,出现第三个。问题是,尽管我Segmented Controller的{​​{1}} IBAction和我的viewWillAppear函数调用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 } } ,但上面的两个版本都没有重新加载。

单击单元格后,相应的信息会显示出来并且一切正常,但直到我单击该行为止。是什么给了什么?

我的MainVC.swift文件

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

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

{{1}}

1 个答案:

答案 0 :(得分:4)

问题是您在后台线程上设置了单元格的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
}