所以我有一个正在填充用户帖子的数组,然后我想根据帖子的类型在tableview中填充它。因此有2种不同的细胞类型。当我运行它时,它显示2个单元格,但第三个单元格被隐藏,直到你拉下tableview。下拉后,cellforrowat再次被调用并正常工作。但是,单元格无序,将其置于顶部而不是应该是第三个单元格。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//print("postArr here", postsArr)
print(postID)
//print("******0", postsArr[0])
//print("******1", postsArr[1])
//print("******2", postsArr[2])
print("in tView")
//timesRun += 1
//let numPosts = postID.count
//let spot = (postID.count - postID.count) + timesRun
print("id count:", postID.count)
print("timeRun:", timesRun)
print("spot is:", timesRun)
if timesRun >= postsArr.count{
timesRun = 0
//tag = 0
}
let spot = postsArr[timesRun]
if spot["postType"] as! Bool == true {
let cell = tableView.dequeueReusableCell(withIdentifier: "forSaleListingCell") as! forSaleListingCell
cell.listingTitle.text = (spot["title"] as! String)
cell.favCount.text = String(spot["numOfFavs"] as! Int)
//need to add the relist stuff
let url = spot["pic"] as! String
cell.postImage.loadImageWithCache(urlString: url)
cell.editBtn.tag = timesRun
cell.commentsBtn.tag = timesRun
cell.editBtn.addTarget(self, action: #selector(forSaleEditPressed(sender:)), for: .touchUpInside)
cell.commentsBtn.addTarget(self, action: #selector(forSaleCommentsPressed(sender:)), for: .touchUpInside)
//cell.edit.addTarget(self, action: #selector(ClassName.FunctionName.buttonTapped), for: .touchUpInside)
postHeight = 127.0
timesRun += 1
saveArr = postsArr[0]
//postsArr.remove(at: 0)
return cell
}
else{
let ISOCell = tableView.dequeueReusableCell(withIdentifier: "ISOListingCell") as! ISOListingCell
let url = spot["pic"] as! String
ISOCell.ISOTitle.text = (spot["title"] as! String)
ISOCell.editListingBtn.tag = timesRun
ISOCell.postImage.loadImageWithCache(urlString: url)
ISOCell.editListingBtn.addTarget(self, action: #selector(ISOEditPressed(sender:)), for: .touchUpInside) //this works with func in this class
postHeight = 80.0
timesRun += 1
saveArr = postsArr[0]
//postsArr.remove(at: 0)
return ISOCell
}
// }
// postsArr.append(forSaleArr)
// postsArr.append(ISOArr)
//timesRun = 0
//viewListView.reloadData()
print("Whya re you here")
let cell = tableView.dequeueReusableCell(withIdentifier: "forSaleListingCell") as! forSaleListingCell
cell.listingTitle.text = "error"
return cell //a blank cell here.
}
我将非常感谢有关如何实现这一目标或更好方法的任何提示。提前致谢。
更新1: Pic 1是你最初看到的,当你下拉视图时,它会转到图2.我希望它看起来像pic 2,除了sell2作为第三个单元格。图3是如果你将单元推出视线并且当它向下拉回时它会重新加载一个新单元的情况。我希望细胞保持有序。
override func viewDidLoad() {
super.viewDidLoad()
self.viewListView.separatorStyle = .none
viewListView.delegate = self
viewListView.dataSource = self
// setupCells()
//viewListView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
timesRun = 0
setupCells()
viewListView.reloadData()
}
func setupCells(){
let ref = Database.database().reference()
let uid = Auth.auth().currentUser?.uid
let userForSale = ref.child("users").child(uid!).child("posts").child("forSalePosts")
let userISO = ref.child("users").child(uid!).child("posts").child("ISOPost")
let user = ref.child("users").child(uid!).child("posts")
user.observeSingleEvent(of: .value, with: { snapshot in
guard let dic = snapshot.value as? [String: Any] else {
return
}
for task in snapshot.children {
guard let taskSnapshot = task as? DataSnapshot else {
continue
}
let id = taskSnapshot.key
//print(id)
let snapshot2 = task as! DataSnapshot
for task2 in snapshot2.children{
guard let taskSnapshot2 = task2 as? DataSnapshot else {
continue
}
let arr = taskSnapshot2.value as! [String: Any]
let pID = taskSnapshot2.key
self.postID.append(pID)
print("id is...", id)
if id == "ISOPost"{
let arr2 = taskSnapshot2.value as! [String: Any]
let dicPic = arr2["postImages"] as! [String: Any]
print("tit", arr2["title"])
let holdArr = ["title": arr2["title"]!, "pic": dicPic["ISOUrl1"]!, "postType": arr2["postType"]!, "postID": pID]
//print(holdArr["postType"])
self.postsArr.append(holdArr)
}
if id == "forSalePosts" {
let arr2 = taskSnapshot2.value as! [String: Any]
let dicPic = arr2["postImages"] as! [String: Any]
let holdArr = ["title": arr["title"]!, "numOfFavs": arr["numOfFavs"]!, "pic": dicPic["forSaleUrl1"]!, "postType": arr["postType"]!, "postID": pID]
self.postsArr.append(holdArr)
}
}
}
self.viewListView.reloadData()
})
}
更新2:
我已将cellforrowat更新为indexpath.row而不是timesRun新屏幕截图如下。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//print("postArr here", postsArr)
print(postID)
//print("******0", postsArr[0])
//print("******1", postsArr[1])
//print("******2", postsArr[2])
print("in tView")
//timesRun += 1
//let numPosts = postID.count
//let spot = (postID.count - postID.count) + timesRun
print("id count:", postID.count)
print("timeRun:", timesRun)
print("spot is:", timesRun)
if timesRun >= postsArr.count{
timesRun = 0
//tag = 0
}
let spot = postsArr[indexPath.row]
if spot["postType"] as! Bool == true {
let cell = tableView.dequeueReusableCell(withIdentifier: "forSaleListingCell") as! forSaleListingCell
cell.listingTitle.text = (spot["title"] as! String)
cell.favCount.text = String(spot["numOfFavs"] as! Int)
//need to add the relist stuff
let url = spot["pic"] as! String
cell.postImage.loadImageWithCache(urlString: url)
cell.editBtn.tag = timesRun
cell.commentsBtn.tag = timesRun
cell.editBtn.addTarget(self, action: #selector(forSaleEditPressed(sender:)), for: .touchUpInside)
cell.commentsBtn.addTarget(self, action: #selector(forSaleCommentsPressed(sender:)), for: .touchUpInside)
//cell.edit.addTarget(self, action: #selector(ClassName.FunctionName.buttonTapped), for: .touchUpInside)
postHeight = 127.0
timesRun += 1
saveArr = postsArr[0]
//postsArr.remove(at: 0)
return cell
}
else{
let ISOCell = tableView.dequeueReusableCell(withIdentifier: "ISOListingCell") as! ISOListingCell
let url = spot["pic"] as! String
ISOCell.ISOTitle.text = (spot["title"] as! String)
ISOCell.editListingBtn.tag = timesRun
ISOCell.postImage.loadImageWithCache(urlString: url)
ISOCell.editListingBtn.addTarget(self, action: #selector(ISOEditPressed(sender:)), for: .touchUpInside) //this works with func in this class
postHeight = 80.0
timesRun += 1
saveArr = postsArr[0]
//postsArr.remove(at: 0)
return ISOCell
}
// }
// postsArr.append(forSaleArr)
// postsArr.append(ISOArr)
//timesRun = 0
//viewListView.reloadData()
print("Whya re you here")
let cell = tableView.dequeueReusableCell(withIdentifier: "forSaleListingCell") as! forSaleListingCell
cell.listingTitle.text = "error"
return cell //a blank cell here.
}
因此,图像1是您第一次进入查看列表页面时获得的图像。当您下拉视图时,您将获得图像2.当您第一次进入页面时,我希望它看起来如何。
更新3 这是我的文件中处理tableview的所有代码。我认为约束会解决问题,因为我忘了添加它们。但是,我仍然有同样的问题,必须拉下视图。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(postHeight)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postID.count //return the num of posts in the users profile.
}
答案 0 :(得分:0)
首先,我要感谢3Stud1ant3没有他的帮助,我可能永远不会发现这一点。
我最初将postHeight变量设置为0,期望在调用cellforrowat之后才调用heightForRowAt函数,其中我根据单元格设置高度。我将变量postHeight从0更改为80.0,现在它按预期正常运行。
var postHeight = 80.0
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat(postHeight)
}