Метка ячейки не улавливает значение в табличном представлении - PullRequest
0 голосов
/ 26 февраля 2019

У меня есть два массива чисел, которые я пытаюсь сопоставить, а затем хочу изменить фон метки ячейки в делегате cellForRowAtIndexPath в TableView.Но это не улавливает мое состояние, даже если оно соответствует требованиям.Это мой код:

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 Он не меняет цвет для метки ячейки

1 Ответ

0 голосов
/ 26 февраля 2019

Я считаю, что проблема в том, что вы циклически просматриваете весь массив каждый раз, когда получаете запрос на ячейку.То, что вы, вероятно, хотите сделать, это получить number для indexPath.row и перебрать 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
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...