Ios 如何在弹出子视图中拥有UITableView?
我有一个具有适当协议的MainWindowViewController。我还在MainWindowViewController中实现了dataSouce方法Ios 如何在弹出子视图中拥有UITableView?,ios,objective-c,Ios,Objective C,我有一个具有适当协议的MainWindowViewController。我还在MainWindowViewController中实现了dataSouce方法 @interface MainWindowController : UIViewController < UITableViewDelegate, UITableViewDataSource, UAModalPanelDelegate, UIGestureRecognizerDelegate> self.friendsTabl
@interface MainWindowController : UIViewController < UITableViewDelegate, UITableViewDataSource, UAModalPanelDelegate, UIGestureRecognizerDelegate>
self.friendsTableView.delegate = self;
self.friendsTableView.dataSource = self;
- (IBAction)on_friends:(id)sender {
if (self.friendsPopUpView == nil) {
[[NSBundle mainBundle] loadNibNamed:@"FriendsPopUpView_iPhone" owner:self options:nil];
[self.view addSubview:self.friendsPopUpView];
UIButton* clickedButton = (UIButton*) sender;
CGRect sFrame = CGRectMake(clickedButton.frame.origin.x-100, clickedButton.frame.origin.y,
self.friendsPopUpView.frame.size.width,
self.friendsPopUpView.frame.size.height);
self.friendsPopUpView.frame = sFrame;
}
}
应该发生的是我按下“朋友”按钮。加载了xib文件名FriendsPopUpView\u iPhone,它应该会显示朋友的UITableView
。但是friendsPopUpView的tableview显示为空行。我做错了什么
FriendsPopUpView_iPhone.xib包含一个UITableView
。
friendsTableView是在FriendsPopUpView_iPhone.xib中创建的tableview的一个出口。
friendsPopUpView是friendsPopUpView_iPhone.xib中视图的一个UIView
插座。
以下是连接到MainWindowController上的friend按钮的操作
@interface MainWindowController : UIViewController < UITableViewDelegate, UITableViewDataSource, UAModalPanelDelegate, UIGestureRecognizerDelegate>
self.friendsTableView.delegate = self;
self.friendsTableView.dataSource = self;
- (IBAction)on_friends:(id)sender {
if (self.friendsPopUpView == nil) {
[[NSBundle mainBundle] loadNibNamed:@"FriendsPopUpView_iPhone" owner:self options:nil];
[self.view addSubview:self.friendsPopUpView];
UIButton* clickedButton = (UIButton*) sender;
CGRect sFrame = CGRectMake(clickedButton.frame.origin.x-100, clickedButton.frame.origin.y,
self.friendsPopUpView.frame.size.width,
self.friendsPopUpView.frame.size.height);
self.friendsPopUpView.frame = sFrame;
}
}
弹出视图nib是否包含连接到MainWindowViewController类的插座(如self.friendsPopUpView)?为了使任何东西都能起作用,它必须是 在表视图存在之前,不能设置委托和数据源。当MainWindowViewController viewDidLoad启动时,它不存在。若要在代码中设置委托和数据源,请在加载nib后,在表存在后进行设置 如果您将其他出口(如friendsPopUp和friendsTableView)设置为nib出口(连接到“文件所有者”,您可以将其设置为MainWindowViewController),则可以以相同的方式设置委托和数据源,无需代码。否则,请在加载nib后在代码中执行
- (IBAction)on_friends:(id)sender {
if (self.friendsPopUpView == nil) {
[[NSBundle mainBundle] loadNibNamed:@"FriendsPopUpView_iPhone" owner:self options:nil];
// assuming you have a friendsPopUpView outlet setup in the nib
// also assuming you have a friendsTableView outlet setup in the nib, both of these connected
// now this will work
self.friendsTableView.delegate = self;
self.friendsTableView.dataSource = self;