簡體   English   中英

單元格標簽在表視圖中未捕獲值

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

我有兩個要匹配的數字數組,然后想要在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它不會更改單元格標簽的顏色

我相信問題在於,每次收到單元格請求時,您都在遍歷整個數組 您可能想要做的是獲取indexPath.rownumber ,並循環遍歷onlineUserArray以查找狀態。

這樣的事情(我不知道您的確切數據結構,但這應該在正確的位置):

    // 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