我面臨這個問題,我沒有得到任何明確的答案。我有一個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()
}
}
可能是什麼問題?
你是不是可選擇的意思是什麼?沒有選擇的外觀或didSelectRowAtIndexPath未被調用? – chengsam
對不起我的不好,didSelectRowAtIndexPath沒有得到調用 –
你是否正確地設置tableview的委託和數據源在viewdidload或其他地方?只是確認。 – Tuhin