我有一個問題,我有一個控制器,並在viewdidload,我嘗試加載從xib文件創建的子視圖。SWIFT TableView沒有響應
我的「自定義」子視圖添加到我的第一個控制器,但tableview沒有響應...我的意思是它不滾動,當我點擊它,沒有任何反應......(我沒有還實現了單擊單元格時觸發動作的方法,但單擊時單元格不會突出顯示)。
下面的代碼:
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let v1 = MyViewTest(frame: CGRectZero)
v1.tableView.dataSource = self
v1.tableView.delegate = self
self.view.addSubview(v1)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//datasource method returning the what cell contains
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
//reusing the previous scrolled cells from table view(if any)
//cell.textLabel?.text = array[indexPath.row]
cell.textLabel?.text = "test"
//passing each elements of the array to cell
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//datasource method returning no. of rows
return 14
}
}
在這裏的廈門國際銀行文件中的代碼在MyViewTest
class MyViewTest: UIView {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
var view:UIView!
let nibName:String = "MyViewTest"
override init(frame: CGRect) {
super.init(frame:frame)
setup()
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)
setup()
}
func setup() {
view = loadViewFromNib()
let screenSize: CGRect = UIScreen.mainScreen().bounds
let sWidth = screenSize.width
let sHeight = screenSize.height
let xPos = sWidth/2-(view.bounds.width/2)
let yPos = sHeight/2-(view.bounds.height/2)
view.frame = CGRectMake(xPos, yPos, view.bounds.width, view.bounds.height)
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let mview = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return mview
}
}
,我只有在帶有標籤的觀點和我的tableview的廈門國際銀行文件相關聯。與它的類(在標識符中)。
如果你知道爲什麼我的表視圖沒有工作,那麼將是巨大的THX!
它看起來像v1可能會得到所有的觸摸,因爲它在tableview之上。將v1的背景設置爲某種顏色(或者使用視圖調試器)並檢查它。 – JDM
我添加了我的xib文件的打印屏幕,我已經有了v1的顏色... –
您似乎將MyTestView的框架設置爲CGRectZero。然後將您的表格添加爲此視圖的子視圖並設置了框架大小。由於MyTestView具有0寬度和高度,並且視圖的默認設置是不剪切子視圖,我想你可以看到表格但不能點擊它。嘗試將MyTestView的框架設置爲屏幕尺寸? –