2016-09-17 59 views
0

我有一個tableView在我的項目中成功運行。但是,我有一個問題,選擇單元格和更新我的textView連接到tableview在同一viewController.My textView只更新當我做長按on tableViewCell.I想要每次更新textView時單元格被按下。我相信,當我按下單元格取消選擇本身。我的部分代碼如下..TableView didSelect在Swift中響應問題

注: 我用我的不同項目下面的代碼工作沒有issue..I我不知道什麼是錯與此的tableview ....

謝謝提前....

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let CellIdentifier: String = "fontCell" 
    let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) 

    let fontName: String = self.fontName[indexPath.row] as! String 

    UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveEaseInOut, animations: {() -> Void in 

    cell.textLabel?.text = fontName 
    cell.textLabel!.font = UIFont(name: self.fontName[indexPath.row] as! String, size: 25) 

    }, completion: nil) 

    cell.backgroundColor = UIColor.clearColor() 
    cell.textLabel?.textAlignment = .Center 
    cell.textLabel?.textColor = UIColor.whiteColor() 
    cell.textLabel?.sizeToFit() 
    cell.textLabel?.adjustsFontSizeToFitWidth = true 
    cell.selectionStyle = .Default 

    return cell 
} 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    dispatch_async(dispatch_get_main_queue(), {() -> Void in 
    let fontName: String = self.fontName[indexPath.row] as! String 

    self.textView.font = UIFont(name: fontName, size: 20) 
    }) 

} 

回答

1

didSelectRowAtIndexPath應該已經在主線程,以便去除dispatch_async和公正運行這樣的代碼應該是罰款稱爲

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let fontName: String = self.fontName[indexPath.row] as! String 
    self.textView.font = UIFont(name: fontName, size: 20) 
} 

如果你已經添加一個手勢識別器到你的tableView來處理長按(或別的東西),可能會干擾你的其他水龍頭。 嘗試在識別器上將cancelsTouchesInView設置爲false。

tapGestureRecognizer.cancelsTouchesInView = false 
+0

謝謝Moriya。我已經嘗試過你的解決方案。但沒有運氣這就是爲什麼我嘗試dispatch_async。我相信我的tableview控制器物理設置,idont知道有什麼問題呢...... – Joe

+0

didSelectRowAtIndexPath中的代碼是否運行?如果你設置了一個斷點,當你選擇一行時它會停止嗎? – Moriya

+0

謝謝。當我按住單元格/長按按鈕時,代碼工作正常。但是,當我按下單元格的單元格選擇發生,只要提起選定的手指單元格消失,而不是更新目標視圖...並且我不能在代碼中看到任何斷點... – Joe