gpt4 book ai didi

ios - UITableView 中的两个自定义 tableViewCell

转载 作者:行者123 更新时间:2023-11-30 13:16:50 25 4
gpt4 key购买 nike

我正在尝试创建一个联系人页面,您可以在其中查看所有联系人,并在收到好友请求时显示好友请求单元格,但在您没有好友请求时则不显示。目前,两个自定义单元都工作正常。我遇到的问题是 contactRequestTableViewCell 与 contactListTableViewCell 的第一个单元格重叠。

我研究了有关两个自定义 tableviewcell 的其他问题,但没有一个问题与我面临的问题相同。

enter image description here

This is what my storyboard looks like

这是我目前的执行代码,我在 TableView 中返回 2 个部分。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactListTableViewCell
let requestCell = tableView.dequeueReusableCellWithIdentifier("requestCell", forIndexPath: indexPath) as! ContactRequestsTableViewCell


let user = OneRoster.userFromRosterAtIndexPath(indexPath: indexPath)


if (amountOfBuddyRequests > 0) {


if (indexPath.section == 0) {

requestCell.hidden = false
cell.hidden = false
requestCell.friendRequestLabel.text = "test"


} else if (indexPath.section >= 1) {

cell.contactNameLabel!.text = user.displayName;
cell.contactHandleLabel!.text = "@ " + beautifyJID(user.jidStr)
cell.contactHandleLabel!.textColor = UIColor.grayColor()

OneChat.sharedInstance.configurePhotoForImageView(cell.imageView!, user: user)
}

return cell;
}

else { // if buddy requests == 0
requestCell.hidden = true



cell.contactNameLabel!.text = user.displayName;
cell.contactHandleLabel!.text = "@ " + beautifyJID(user.jidStr)
cell.contactHandleLabel!.textColor = UIColor.grayColor()

print ("This is how many unreadMessages it has \(user.unreadMessages)")

// If there is unread messages for a person highlight it blue
// However this feature isn't working right now due to unreadMessages bug
if user.unreadMessages.intValue > 0 {
cell.backgroundColor = .blueColor()
} else {
cell.backgroundColor = .whiteColor()
}

OneChat.sharedInstance.configurePhotoForCell(cell, user: user)

return cell;
}


}

enter image description here

这是我现在的当前输出,我的具有“test”的单元格覆盖了其他 contactListTableViewCells。

最佳答案

函数 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 应该始终返回 一个且唯一的一个 TableViewCell 您想要在 indexPath,因此您不希望始终返回 ContactListTableViewCell 类型的 cell

根据documentationcellForRowAtIndexPath tableView 方法要求索引路径处的单元格,这意味着从字面上看,某一部分的某一行只能有一个单元格,因此返回两个单元格不是一种选择。

我建议您使用两个数组来存储请求和联系人信息。例如,您有数组 requestscontacts。然后你可以告诉tableView你想要多少行:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return requests.count + contacts.count
}

然后在cellForRowAtIndexpath中执行如下操作:

override func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row < requests.count {
// return a request cell
}
else {
// return a contact cell
}
}

我在这里只使用一个 tableView 部分。如果您仍然想要两个部分,您可以简单地在 numberOfSections 函数中返回 2,并在 cellForRowAtIndexPath 中为 indexPath.section 添加 if 语句。

希望这有帮助。

关于ios - UITableView 中的两个自定义 tableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38134695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com