ios - 单元格标签无法捕获表格 View 中的值

标签 ios swift uitableview

我有两个数字数组,我试图匹配它们,然后想要更改 TableView 中 cellForRowAtIndexPath 委托(delegate)中单元格标签的背景。但即使条件正确满足,它也没有捕捉到我的条件。这是我的代码:

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

关于ios - 单元格标签无法捕获表格 View 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54889253/

相关文章:

c - 整数不作为对 Swift 中函数的引用传递

iPhone:核心数据保存错误

ios - Swift:如何在动态 UITableView 中使用静态单元格

ios - Swift 自定义 UITableViewCell 不显示数组中的数据

iphone - 通过编码下载文件

iphone - 如何验证文本字段中只允许单个小数点的文本字段?

Swift:有条件地设置私有(private) Ivar

ios - 解释一下 iOS 的构建和编译?

jquery - 当您希望 iPad 显示桌面站点然后对小型移动设备使用媒体查询时,视口(viewport)会出现困惑

ios - 如何在 Swift 中垂直(沿 y 轴)缩放 3D 对象?