[英]Using UIRefreshControl in UIViewController with UITableView
[英]UIRefreshControl in a UIViewController
我有一个UIViewController
,其中有一个UITableView
作为subView
。 我正在尝试在表上添加“刷新”功能。 我有UIRefresh
显示,但它从未调用选择器。 我没有收到发生拉动动作的通知。 我无法使用TableView
控制器,因为我将此视图用作通用视图,因此可以用于多个View控制器
- (void)viewDidLoad{
[super viewDidLoad];
// Refresh control
UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
tableViewController.tableView = self.genericMainTableView;
// Initialise refresh control
self.refreshControl = [[UIRefreshControl alloc] init];
//Adding target to refresh Control object
[self.refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
//Setting attribute Title
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull To Refresh"];
//Adding as subview to tableView
tableViewController.refreshControl = self.refreshControl;
}
- (void)refresh:(UIRefreshControl *)refreshControl {
reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reachability currentReachabilityStatus];
//Not Reachable - No Internet Connection
if (netStatus == NotReachable) {
[[[UIAlertView alloc]initWithTitle:@"Network Error" message:@"Please Check your Network Connection" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]show];
[refreshControl endRefreshing];
}
else{
//My Function Call
[self functionCall];
//Reload the Table
[self.genericMainTableView reloadData];
//set the title while refreshing
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refreshing the TableView"];
//set the date and time of refreshing
NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
[formattedDate setDateFormat:@"MMM d, h:mm a"];
NSString *lastupdated = [NSString stringWithFormat:@"Last Updated on %@",[formattedDate stringFromDate:[NSDate date]]];
refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
//End Refreshing
[refreshControl endRefreshing];
}
}
我在这里做所有事情吗? 它对我不起作用。 请帮助我。
问题出在您的viewDidLoad中。 无需实例化UITableViewController进行刷新控制。 只需将viewDidLoad更改为此
- (void)viewDidLoad{
[super viewDidLoad];
// Initialise refresh control
self.refreshControl = [[UIRefreshControl alloc] init];
//Adding target to refresh Control object
[self.refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
//Setting attribute Title
self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Pull To Refresh"];
//Adding as subview to tableView
[self.genericMainTableView addSubview:self.refreshControl];
}
您必须像这样在viewDidLoad
启动UIRefreshControl
:
// Pull To Refresh Code
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
这是您的选择器handleRefresh:
-(void) handleRefresh:(UIRefreshControl *) refreshControl{
[self performSelector:@selector(yourMethodHere)];
}
另外,请尝试检查对选择器的引用是否正常。 单击选择器,并按住命令按钮。
您只需要一个scorllview并将其设置为refreshcontrol就可以了。 无论您使用哪种控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.