【问题标题】:UITableView in swiftswift中的UITableView
【发布时间】:2016-01-02 17:41:38
【问题描述】:

谁能告诉我我的代码有什么问题我总是在所有单元格中重复相同的数据

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel!.textColor = UIColor.whiteColor()
       let query = PFQuery(className:"book")
    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in
          var contentString:String = String()
        if error == nil {
            print("Success retrieved \(objects!.count) scores.")
            if let objects = objects {
                for object in objects {
               let a:String = object.objectForKey("title") as! String
                contentString = a
            }
        }
              cell.textLabel!.text = contentString
    }}
    return cell
    }

【问题讨论】:

  • 这类似于 stackoverflow.com/questions/34524180/…> 使用 tableview 单元格 id 应该是唯一的

标签: swift uitableview parse-platform


【解决方案1】:

最好将所有数据查询到 viewDidLoad() 或您想要的任何生命周期中。

    var contentString = [String]() //<-- use an array of string instead of a string variable

 override func viewDidLoad()
   {
      super.viewDidLoad()
        let query = PFQuery(className:"book"){ query.findObjectsInBackgroundWithBlock {
    (objects: [PFObject]?, error: NSError?) -> Void in

    if error == nil {
        print("Success retrieved \(objects!.count) scores.")
        if let objects = objects {
            for object in objects 
        {
            let value = object["title"] as! String
            self.contentString.append(value) //<-- save the query value to your array 
        }
        self.tableView.reloadData() //<--- then you reload your UI
      }
   }}
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
   cell.textLabel!.textColor = UIColor.whiteColor()
   cell.textLabel!.text = self.contentString[indexPath.row] //<-- then assign the value into your cell 
   return cell
}

【讨论】:

    【解决方案2】:

    循环内的内容应该是这样的。

    let a:String = object.objectForKey("title") as! String
    contentString += a
    

    我注意到你的块调用总是相同的查询。你能告诉我那里有什么不同吗

     query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in
          var contentString:String = String()
        if error == nil {
            print("Success retrieved \(objects!.count) scores.")
            if let objects = objects {
                for object in objects {
               let a:String = object.objectForKey("title") as! String
                contentString = a
            }
        }
              cell.textLabel!.text = contentString
    }}
    

    【讨论】:

      【解决方案3】:

      cellForRowAtIndex 中的查询很麻烦。想象一下,每次看到一个单元格时,代码都会从服务器请求每一本书。

      将查询放在数据源方法之外(例如viewWillAppear)。查询完成后,用结果初始化一个数组属性(例如self.bookObjects = objects)。

      然后在cellForRowAtIndex...

      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
      cell.textLabel!.textColor = UIColor.whiteColor()
      
      // this is the important idea...
      let object = self.bookObjects[indexPath.row]
      
      let a:String = object.objectForKey("title") as! String
      cell.textLabel!.text = a
      return cell
      

      【讨论】:

        猜你喜欢
        • 2014-07-24
        • 2022-01-24
        • 2016-06-24
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多