下面我试图将数据加载到UITableView中,每当数据在错误的单元格中结束或复制到单元格中时,它应该永远无法访问。我已经删除了很多额外的代码,但仍然遇到了这个问题。我想我遗漏了一些关于UITableViews的基本信息。
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单元格。< / p>
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 :(得分: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
}
答案 1 :(得分: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
}
}