我有两个要匹配的数字数组,然后想要更改TableView
中cellForRowAtIndexPath委托中的单元格标签背景。但是,即使条件正确满足,也无法抓住我的条件。这是我的代码:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if dataLoded {
if isGroupListView {
if localGroupChannelsArr.count > 0 {
noDataFoundView.isHidden = true
}else {
noDataFoundView.isHidden = false
}
return localGroupChannelsArr.count
}
if localSingleChannelsArr.count > 0 {
noDataFoundView.isHidden = true
}else {
noDataFoundView.isHidden = false
}
return localSingleChannelsArr.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell = ChatRoomCellView()
var channelRoom = ChatRoom()
// var onlineStatus = OnlineUser()
if isGroupListView {
channelRoom = localGroupChannelsArr[indexPath.row]
cell = groupChatRoomsList.dequeueReusableCell(withIdentifier: "GroupRoomCellView") as! ChatRoomCellView
}else {
channelRoom = localSingleChannelsArr[indexPath.row]
cell = singleChatRoomsList.dequeueReusableCell(withIdentifier: "SingleRoomCellView") as! ChatRoomCellView
for (phone, number) in zip(numbers, onlineUserArray) {
print(phone)
print(phone,number.phone!)
if phone == number.phone
{
if number.status == "online"
{
print("Matched")
cell.onlineStatusLbl.backgroundColor = #colorLiteral(red: 0.08950354904, green: 0.807744801, blue: 0.07534810156, alpha: 1)
}
else if number.status == "offline"
{
cell.onlineStatusLbl.backgroundColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1)
}
else
{
cell.onlineStatusLbl.backgroundColor = UIColor.clear
}
}
}
}
cell.selectionStyle = .none
cell.delegate = self
cell.myIndexPath = indexPath
cell.chatRoomObj = channelRoom
cell.onlineUser = onlineUserArray
cell.configCell()
return cell
}
现在,当满足条件时,if phone == number.phone
不会更改单元格标签的颜色
答案 0 :(得分:0)
我认为问题在于,每次收到单元格请求时,您都在遍历 整个数组 。您可能想做的就是获取number
的{{1}}并遍历indexPath.row
以查找状态。
这样的事情(我不知道您的确切数据结构,但这应该在正确的位置):
onlineUserArray