2017-10-07 101 views
0

我面臨這個問題,我沒有得到任何明確的答案。我有一個UITableView,它在第一次加載時工作正常,但當它滾動時,它不可選,didSelectRowAtIndexPath永遠不會接到呼叫。此外,我已經在我的手機按鈕,它也不可點擊。但我仍然可以滾動tableview。這是我的代碼:iOS UITableView在滾動時不可選擇

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

可能是什麼問題?

+0

你是不是可選擇的意思是什麼?沒有選擇的外觀或didSelectRowAtIndexPath未被調用? – chengsam

+0

對不起我的不好,didSelectRowAtIndexPath沒有得到調用 –

+0

你是否正確地設置tableview的委託和數據源在viewdidload或其他地方?只是確認。 – Tuhin

回答

0

你正在做的一切都是正確的。

只要檢查您的按鈕框架是否位於您的單元格內。

有時您的細胞高度較低,按鈕超出細胞範圍。

+0

cell.delegate = self in viewDidLoad()? –

0

這也可以在視圖層次 覆蓋默認的觸摸事件與UITapRecognizer S的,因爲刪除任何tap recognizer S設定對UITableviewParent View

+0

我嘗試刪除所有水龍頭識別器,但讓我再試一次 –

0

謝謝大家的幫助。問題出在我用於CustomCell的SwipeCellKit。刪除它解決了它!