2017-06-22 79 views
-1

下面我试图将数据加载到UITableView中,并且每次数据都在错误的单元格中结束,或者复制到应该永远不会访问的单元格中。我已经删除了很多额外的代码,并且仍然遇到这个问题。我想我缺少一些有关UITableViews的基础知识。UITableView将数据加载到错误的单元格中

func setUpFeedTable() { 

       self.tableFeed.isScrollEnabled = true 

       for i in 0 ..< allDictionary.count { 


        let dictionary = allDictionary[i] 


        if dictionary?["type"]! != nil { 
         let l = i 
         print("text", l) 
         self.tableFeed.cellForRow(at: IndexPath(row: l, section: 0))?.imageView?.layer.borderColor = UIColor.black.cgColor 
         self.tableFeed.cellForRow(at: IndexPath(row: l, section: 0))?.imageView?.layer.borderWidth = 1 
         self.tableFeed.cellForRow(at: IndexPath(row: l, section: 0))?.imageView?.layer.cornerRadius = 1 

         //self.tableFeed.cellForRow(at: IndexPath(row: i, section: 0))?.imageView?.clipsToBounds = true 
         self.editProfileButton.imageView?.contentMode = UIViewContentMode.scaleAspectFill 
         //self.tableFeed.reloadData() 
         //self.tableFeed.reloadRows(at: [IndexPath(row: l, section: 0)], with: UITableViewRowAnimation(rawValue: 0)!) 

         self.tableFeed.cellForRow(at: IndexPath(row: l, section: 0))?.imageView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height/2, width: 100, height: 100) 

         self.tableTextOverLays(i: l, type: Int((dictionary?["type"])! as! NSNumber)) 

         //self.tableFeed.reloadRows(at: [IndexPath(row: l, section: 0)], with: UITableViewRowAnimation(rawValue: 0)!) 

         self.tableFeed.reloadDate() 
        } 

        else if (dictionary?["type_"] as! String) == "Image" { 
         print("image", i) 

        } 
        else { 
         print("video") 
        } 



       } 



      } 

      func tableTextOverLays(i: Int, type: Int){ 
       if type == 0{ 
        print("saw type 0", i) 
        self.tableFeed.cellForRow(at: IndexPath(row: i, section: 0))?.imageView?.image = #imageLiteral(resourceName: "Geo-fence-50") 
       } 

       else if type == 1 { 
        self.tableFeed.cellForRow(at: IndexPath(row: i, section: 0))?.imageView?.image = #imageLiteral(resourceName: "Happy-50") 
       } 

       else if type == 2 { 
        self.tableFeed.cellForRow(at: IndexPath(row: i, section: 0))?.imageView?.image = #imageLiteral(resourceName: "Information-50") 
       } 

       else if type == 3 { 
        self.tableFeed.cellForRow(at: IndexPath(row: i, section: 0))?.imageView?.image = #imageLiteral(resourceName: "Home-50") 
       } 
      } 

更新:我已经编辑我的代码,事情正在努力更好(所以谢谢!),但我现在遇到了一个图像是地方的问题都到前两个单元格,然后第4和第第5个细胞。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     if tableView == self.table { 
      return users2.count 

     } 
     else { 
      //print("married barry", tableFeedCount) 
      return tableFeedCount 
     } 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     if tableView == self.table { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as UITableViewCell 
      cell.textLabel?.text = self.users2[indexPath.row].name 
      return cell 
     } 
     else { 
      //print("working?") 
      let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as UITableViewCell 
      let dictionary = allDictionary[indexPath.row] 


      if dictionary?["type"]! != nil { 
       cell.imageView?.layer.borderColor = UIColor.black.cgColor 
       cell.layer.borderWidth = 1 
       cell.imageView?.layer.cornerRadius = 1 


       cell.imageView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height/2, width: 100, height: 100) 

       self.tableTextOverLays(i: indexPath.row, type: Int((dictionary?["type"])! as! NSNumber), cell: cell) 

      } 

      else if (dictionary?["type_"] as! String) == "Image" { 
       print("image") 

      } 
      else { 
       print("video") 
      } 


      return cell 
     } 

    } 
+0

信息数组有没有你是不是想充分利用'tableViewDataSource'协议的具体原因是什么? – AgRizzo

+1

是的,你正在使用错误的方法。 'cellForRowAtIndexPath:'返回已经存在于请求索引处的已格式化的单元格。单元格是哑元容器,当您在列表中滚动时会重新使用/重新填充,因此您无法像这样单独设置属性。初始单元格格式发生在'dequeueReusableCell ...'这里,您可以设置标签,图像,单元格类型。它为'dataSource'中的每个索引创建单元格;没有循环req'd。 页眉/页脚单元格也使用单独的方法。建议查看关于如何使用UITableView的教程,创建自定义的UITableViewCell子类等。 – mc01

回答

0

这是不正确的填充TableView这样。 每个TableView必须与一个DataSource或到UITableViewDelegate,而这一点,例如,应实施的方法,如tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

例如:

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet var tableView: UITableView! 

    var myStrings = ["1", "2", "3", "4", "5"] 

    override func viewDidLoad() { 
     self.tableView.delegate = self 
     self.tableView.dataSource = self 
    } 

    @available(iOS 2.0, *) 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if section == 0 { 
      return 1 
     } 
     return myStrings.count 
    } 

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

    @available(iOS 2.0, *) 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "reuseId", for: indexPath) 
     if indexPath.section == 0 { 
      cell.textLabel?.text = "This is the row in the first section" 
     } else { 
      cell.textLabel?.text = myStrings[indexPath.row] 
     } 

     return cell 
    } 
} 
2

的原因是因为找你试图设置UITableviewCells请求单元格到tableView的元数据,单元格在UITableView协议方法cellforRowAtIndexPath中被重用,这就是为什么您在其他单元格中看到重复或元数据的原因。

您需要做的是在cellforRowAtIndexPath方法中设置每个单元格的元数据。

例如,如果你有这样的

let metadata = ["title1", "title2", "title3"] 

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) 
    let title = metadata[indexPath.row] 
    cell.titleLabel.text = title 
    return cell 
} 
相关问题