Ios UITableView将数据加载到错误的单元格中
下面我尝试将数据加载到UITableView中,每次数据出现在错误的单元格中或复制到单元格中时,它都不应该访问。我已经删除了很多额外的代码,但仍然遇到这个问题。我想我遗漏了一些关于UITableView的基本信息Ios UITableView将数据加载到错误的单元格中,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,下面我尝试将数据加载到UITableView中,每次数据出现在错误的单元格中或复制到单元格中时,它都不应该访问。我已经删除了很多额外的代码,但仍然遇到这个问题。我想我遗漏了一些关于UITableView的基本信息 func setUpFeedTable() { self.tableFeed.isScrollEnabled = true for i in 0 ..< allDictionary.count {
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")
}
}
这样填充
表视图是不对的。
每个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
}
}
原因是,您正在尝试将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
}
您不尝试利用tableViewDataSource
协议有什么具体原因吗?是的,您使用了错误的方法cellforrowatinexpath:
返回已存在于请求索引中的格式化单元格。单元格是哑容器,在滚动列表时会被重复使用/重新填充,因此不能像这样单独设置属性。初始单元格格式设置在dequeueReusableCell…
中,您可以在这里设置标签、图像和单元格类型。它为数据源中的每个索引创建单元格
;不需要循环。页眉/页脚单元格也使用单独的方法。建议阅读有关如何使用UITableView、创建自定义UITableViewCell子类等的教程。
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
}