Ios SWIFT TableView没有响应
我有一个问题,我有一个控制器,在viewdidload中,我尝试加载从xib文件创建的子视图 我的“自定义”子视图很好地添加到了我的第一个控制器中,但tableview没有响应。。。我的意思是它不会滚动,当我点击它时,什么都不会发生。。。(我还没有实现在单击单元格时触发操作的方法,但单击单元格时不会突出显示) 代码如下:Ios SWIFT TableView没有响应,ios,iphone,swift,uitableview,subview,Ios,Iphone,Swift,Uitableview,Subview,我有一个问题,我有一个控制器,在viewdidload中,我尝试加载从xib文件创建的子视图 我的“自定义”子视图很好地添加到了我的第一个控制器中,但tableview没有响应。。。我的意思是它不会滚动,当我点击它时,什么都不会发生。。。(我还没有实现在单击单元格时触发操作的方法,但单击单元格时不会突出显示) 代码如下: class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegat
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
}
}
在xib文件中,我只有一个带有标签的视图,而我的tableview在中。xib文件与其类(在标识符中)相关联
如果你知道为什么我的表视图不工作,那就太好了 问题的出现是因为我用CGRectZero实例化了MyViewTest。例如,如果我使用一个CGRectMake,它的实际宽度和高度都非常好
有关更多详细信息,请参见上面的rory mckinnel post:-)您似乎将MyTestView的框架设置为CGRectZero 然后,将表添加为此视图的子视图,并设置框架大小。由于MyTestView的宽度和高度均为0,并且视图的默认值是不剪裁子视图,我想您可以看到该表,但不能单击它
尝试将MyTestView的框架设置为屏幕大小?由于v1位于tableview的顶部,它看起来可能会获得所有的触动。将v1的背景设置为某种颜色(或使用视图调试器)并进行检查。我为我的xib文件添加了一个打印屏幕,我已经为v1设置了颜色…您似乎将MyTestView的框架设置为CGRectZero。然后,将表添加为此视图的子视图,并设置框架大小。由于MyTestView的宽度和高度均为0,并且视图的默认值是不剪裁子视图,我想您可以看到该表,但不能单击它。尝试将MyTestView的框架设置为屏幕大小?事实上,我也在apple开发者论坛上发布了我的问题,这正是我得到的答案,你是对的。我是来写解决方案的,但你已经给出了。无论如何,谢谢你回答我,因为我对此非常绝望,你的回答会帮助我的!很乐意帮忙。我贴了一封回信。