滚动时无法选择iOS UITableView

时间:2017-10-07 08:30:13

标签: ios swift

我正面临这个问题,我没有得到任何明确的答案。我有一个UITableView,它在第一次加载时工作正常,但滚动时,它是不可选择的,didSelectRowAtIndexPath永远不会接到电话。我的单元格中也有按钮,也无法点击。但我仍然可以滚动tableview。这是我的代码:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell : ContactTableViewCell = tableView.dequeueReusableCell(withIdentifier: "ContactCell") as! ContactTableViewCell

        cell.selectionStyle = .none
        cell.backgroundColor = UIColor.clear
        cell.delegate = self

        let randomNumber = Int(arc4random_uniform(UInt32(colorsArray!.count)))

        if(!shouldUseFilteredResults) {

            cell.setFilteredContents(_with: addressBookArray![indexPath.row], theme: themeStyle!, randomColor: colorsArray![randomNumber])
        }
        else {

            cell.setFilteredContents(_with: filteredAddressBook[indexPath.row], theme: themeStyle!, randomColor: colorsArray![randomNumber])

            cell.contactNameLabel.attributedText = self.highlightSearchResult(resultString: cell.contactNameLabel.text!)
        }


        return cell
    }

在单元格中:

public func setFilteredContents (_with contact : [String : Any], theme : Theme, randomColor : UIColor)
    {
        contactDictionary = contact;

        contactNameLabel.text = String(describing: contact["FullName"]!)

        self.setTheme(theme: theme, randomColor: randomColor)

        if (contact["ImageDataString"] != nil)
        {
            let imageData = Data(base64Encoded: contact["ImageDataString"] as! String)
            let image = UIImage(data: imageData!)

            let imageView = UIImageView(frame: thumbnailView.bounds)
            imageView.contentMode = .scaleAspectFill
            imageView.layer.masksToBounds = true
            imageView.layer.cornerRadius = CGFloat(imageView.height()/2)
            imageView.image = image
            thumbnailView.addSubview(imageView)
        }
        else {

            self.setThumbnail(fullName: contactNameLabel.text!)
        }
    }

 private func setThumbnail(fullName : String)
    {
        let vibrancy = UIVibrancyEffect(blurEffect: UIBlurEffect(style: .light))
        let backgroundView = UIVisualEffectView(effect: vibrancy)
        backgroundView.frame = thumbnailView.bounds
        thumbnailView.addSubview(backgroundView)

        let thumbNailLabel = UILabel(frame: thumbnailView.bounds)
        thumbNailLabel.font = UIFont.systemFont(ofSize: 13)
        thumbNailLabel.textColor = UIColor.white
        thumbNailLabel.textAlignment = .center

        var initialLettersString = String()
        let wordsArray = fullName.components(separatedBy: NSCharacterSet.whitespaces)

        for word in wordsArray
        {
            if(word != "")
            {
                initialLettersString.append(String(describing: word.characters.first!))
            }
        }

        if(initialLettersString.characters.count == 1)
        {
            initialLettersString =  String(initialLettersString[..<initialLettersString.index(initialLettersString.startIndex, offsetBy: 1)]).uppercased()
        }
        if(initialLettersString.characters.count > 1)
        {
            initialLettersString = String(initialLettersString[..<initialLettersString.index(initialLettersString.startIndex, offsetBy: 2)]).uppercased()
        }

        thumbNailLabel.text = initialLettersString as String

        backgroundView.contentView.addSubview(thumbNailLabel)
    }



private func setTheme(theme : Theme, randomColor : UIColor)
    {
        let isContactFavorite = contactDictionary?[Constants.TTCIsContactFavoriteKey] as? Bool

        thumbnailView.backgroundColor = randomColor

        if(theme == Theme.Light)
        {
            contactNameLabel.textColor = UIColor.black
            lineView.backgroundColor = UIColor.lightGray

            if(!isContactFavorite!)
            {
                favoritesButton.setImage(UIImage(named: "favorite_dark.png"), for: .normal)
            }
            else
            {
                favoritesButton.setImage(UIImage(named: "favorite.png"), for: .normal)
            }
        }
        else if(theme == Theme.Dark)
        {
            contactNameLabel.textColor = UIColor.white
            lineView.backgroundColor = UIColor.gray

            if(!isContactFavorite!)
            {
                favoritesButton.setImage(UIImage(named: "favorite_white.png"), for: .normal)
            }
            else
            {
                favoritesButton.setImage(UIImage(named: "favorite.png"), for: .normal)
            }
        }

        for view in thumbnailView.subviews
        {
            view.removeFromSuperview()
        }
    }

自定义单元格: Custom Cell

可能是什么问题?

3 个答案:

答案 0 :(得分:0)

你正在做的一切。

只需检查您的按钮框架是否在您的单元格内。

有时候你的细胞高度较小,按钮超出细胞界限。

答案 1 :(得分:0)

这也可能是因为在视图层次结构中覆盖UITapRecognizer s的默认触摸事件 删除设置为tap recognizer UITableview

的所有Parent View

答案 2 :(得分:0)

感谢大家的帮助。问题出现在我用于CustomCell的SwipeCellKit。删除它解决了它!