我正在尝试在我的应用程序中实现 UIRefreshControl
。 我有一个 xib 文件,并在空的 nib 文件中添加了一个 UITableViewController
,并将刷新属性设置为“启用”。 同时,我已经在 viewDidLoad
中添加了代码和自定义刷新方法。 问题是我有一个错误,找不到任何信息.... 在我的 viewDidLoad
中,我得到了“Property 'refreshControl
' not found on object of type ViewController
”。
- (void)viewDidLoad{
[super viewDidLoad];
self.myTableView =
[[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
[self.view addSubview:self.myTableView];
UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[refresh addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged];
self.refreshControl = refresh;
}
-(void)refreshView:(UIRefreshControl *)refresh {
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];
// custom refresh logic would be placed here...
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM d, h:mm a"];
NSString *lastUpdated = [NSString stringWithFormat:@"Last updated on %@",
[formatter stringFromDate:[NSDate date]]];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdated];
[refresh endRefreshing];
}
我不知道为什么那个属性不可用……我错过了什么吗?
看起来我需要在我的ViewController.h
文件中继承UITableViewController
。如果我已经有了UITableView
,如何同时继承两者呢?如果我将代码从ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
更改为ViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
,那么我会收到一个错误:
error: NSInternalInconsistencyException',
reason: '-[UITableViewController loadView] loaded the "ViewController_iPhone" nib but didn't get a UITableView.'
UIRefreshControl
的唯一受支持的方法是与UITableViewController
一起使用。 - Dave DeLongUIRefreshControl
到UITableView
的方法。 - Dave DeLongUITableViewController.refreshControl
属性。像这样手动添加到UITableView
是危险的,可能会在未来的 iOS 版本中出现问题。 - Samah