[英]Swift-app crashes on row selection in UITableView
我在Swift中的UITableView遇到問題。 只要我不實現用於處理單元格選擇的委托功能,它就可以正常工作。
這是我非常簡單的類,它充當表視圖委托和數據源:
import UIKit
class TableViewService : NSObject, UITableViewDataSource, UITableViewDelegate {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if(indexPath.row == 0) {
cell.textLabel?.text = "One"
} else {
cell.textLabel?.text = "Two"
}
cell.selectionStyle = .None
return cell;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
log.debug("Row selceted")
}
}
一旦我在表視圖中選擇一行,應用程序就會崩潰,並在objc_msgSend中出現EXEC_BAD_ACCESS錯誤-控制台中沒有記錄錯誤,並且如果我在didSelectRowAtIndexPath函數上設置了一個斷點,它將永遠不會被觸發。
有什么想法我缺少或做錯了嗎?
這是我毫無問題地創建項目所采取的步驟。 如果您錯過某個地方的某個步驟,也許會有所幫助?:
1)在情節UITableView
中將UITableView
設置為ViewController
的子視圖
2)將代碼添加到ViewController
(您的自定義類)
class ViewController: UIViewController {
@IBOutlet var tableView:UITableView!
let tableService:TableViewService = TableViewService()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = tableService
tableView.dataSource = tableService
}
}
3)確保插座在ViewController
和tableView
之間連接
4) TableViewService
類是從上面的代碼復制/粘貼的(我使用println()
而不是log.debug
)
如果我確定這三件事,那么我就能看到委托方法的調用沒有崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.