简体   繁体   English

单元格标签在表视图中未捕获值

[英]Cell label Does not catching value in table view

I have two arrays of number which i'm trying to match and then want to change background of cell label in cellForRowAtIndexPath delegate in TableView . 我有两个要匹配的数字数组,然后想要在TableView cellForRowAtIndexPath委托中更改单元格标签的背景。 But it is not catching my condition even when the condition meets correctly. 但是,即使条件正确满足,也无法抓住我的条件。 This is my code: 这是我的代码:

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
}

Now when it meets the condition, if phone == number.phone It does not change color for the cell label 现在,当满足条件时, if phone == number.phone它不会更改单元格标签的颜色

I believe the problem is that you are looping through the entire array every time you get a request for a cell. 我相信问题在于,每次收到单元格请求时,您都在遍历整个数组 What you probably want to do is get the number for the indexPath.row and loop through the onlineUserArray to find the status. 您可能想要做的是获取indexPath.rownumber ,并循环遍历onlineUserArray以查找状态。

Something like this (I don't know your exact data structure, but this should be along the right lines): 这样的事情(我不知道您的确切数据结构,但这应该在正确的位置):

    // inside cellForRowAt

    cell = singleChatRoomsList.dequeueReusableCell(withIdentifier: "SingleRoomCellView") as! ChatRoomCellView

    // just to make it easy to see what's going on in the debug console
    print(indexPath)

    let phone = numbers[indexPath.row]

    for number in onlineUserArray
    {
        print(number)
        if number.phone == phone
        {
            if number.status == 1
            {
                print("Matched")
                cell.theLabel.backgroundColor = #colorLiteral(red: 0.08950354904, green: 0.807744801, blue: 0.07534810156, alpha: 1)
            }
            else if number.status == 0
            {
                cell.theLabel.backgroundColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1)
            }
            else
            {
                cell.theLabel.backgroundColor = UIColor.clear
            }
            print("found a match, so break out of the loop")
            break
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM