9

我正在尝试在我的应用程序中实现拉动刷新功能。该架构是这样的,UITableView里面有一个UIViewController. 我希望能够在下拉时刷新表格视图。我在方法中尝试了下面的代码viewDidLoad,但它不起作用。谁能告诉我我在实施中哪里错了?

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
    refresh.tintColor = [UIColor grayColor];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];
    [self.vrnTable addSubview:refresh];
4

11 回答 11

15

由于您不能使用 aUITableViewController而不是UIViewController,请尝试这样做:

UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.vrnTable;

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.tintColor = [UIColor grayColor];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[self.refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];

tableViewController.refreshControl = self.refresh;

希望这可以帮助!

于 2013-11-22T07:03:45.200 回答
8
-(void)viewDidLoad
{
   UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
    //[self.mytable addSubview:refreshControl];
    UITableViewController *tableViewController = [[UITableViewController alloc] init];
    tableViewController.tableView = self.mytable;
    tableViewController.refreshControl = refreshControl;
}

-(void)refreshData
{
    //Put your logic here


   //reload table & remove refreshing image
   UITableViewController *tableViewController = [[UITableViewController alloc] init];
   tableViewController.tableView = self.mytable;
   [self.mytable reloadData];
   [tableViewController.refreshControl endRefreshing];
}
于 2014-06-27T06:26:46.333 回答
3

更新了 Swift 1.2 的答案

    var refreshControl = UIRefreshControl()
    refreshControl.backgroundColor = blue
    refreshControl.tintColor = UIColor.whiteColor()
    refreshControl.addTarget(self, action: Selector("yourFunctionHere"), forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refreshControl)
于 2015-05-04T22:57:51.607 回答
2

你可以在这里看到: UIViewController 中的 UIRefreshControl (with UITableView)

@interface MyViewController ()
{
    UIRefreshControl *refreshControl;
}
  @property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *refreshView = [[UIView alloc] initWithFrame:CGRectMake(0, 55, 0, 0)];
    [self.tableView insertSubview:refreshView atIndex:0]; //the tableView is a IBOutlet

    refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.tintColor = [UIColor redColor];
    [refreshControl addTarget:self action:@selector(reloadDatas) forControlEvents:UIControlEventValueChanged];
    /* NSMutableAttributedString *refreshString = [[NSMutableAttributedString alloc] initWithString:@"Pull To Refresh"];
    [refreshString addAttributes:@{NSForegroundColorAttributeName : [UIColor grayColor]} range:NSMakeRange(0, refreshString.length)];
    refreshControl.attributedTitle = refreshString; */
    [refreshView addSubview:refreshControl];
}

-(void)reloadDatas
{
   //update here...

   [refreshControl endRefreshing];
}

@end

http://www.g8production.com/post/79514553078/ios7-and-uirefreshcontrol-in-uiviewcontroller-with

于 2014-04-13T11:03:28.670 回答
2

如果您的应用仅支持 iOS 6(及更高版本):我建议UIRefreshControl

如果也支持 iOS 5,可以使用 https://github.com/enormego/EGOTableViewPullRefresh

于 2014-06-27T08:31:02.697 回答
2

对于 Swift 3 和 iOS 向后兼容

var refreshControl = UIRefreshControl()
let string = "Pull to refresh"
let attributedString = NSMutableAttributedString(string: string)
    attributedString.addAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 16)),NSForegroundColorAttributeName:UIColor.white], range: NSRange.init(location: 0, length: string.characters.count))
self.refreshControl.attributedTitle = attributedString
self.refreshControl.tintColor = UIColor.white
self.refreshControl.addTarget(self,
                             action: #selector(self.pulledDownForRefresh),
                             for: .valueChanged)
if #available(iOS 10.0, *) {
    self.accountSummaryTableView.refreshControl = refreshControl
} else {
    self.accountSummaryTableView.addSubview(refreshControl)
}

func pulledDownForRefresh() {
     //do some opertaion and then call
    self.refreshControl.endRefreshing()
}
于 2017-04-28T09:15:30.280 回答
0

我认为您需要设置 UITableView 的刷新控件。我需要查看更多代码和视图结构来诊断问题。

这是objective-c和swift的教程:http ://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

于 2015-02-27T15:10:53.963 回答
0

对于斯威夫特 3:

var refreshControl: UIRefreshControl!

override func viewDidLoad() {
    super.viewDidLoad()

    self.refreshControl = UIRefreshControl()

    self.refreshControl.tintColor = UIColor.black
    self.refreshControl.addTarget(self,
                                  action: #selector(ViewController.pullToRefreshHandler),
                                  for: .valueChanged)

    self.tableView.addSubview(self.refreshControl)
}

@objc func pullToRefreshHandler() {
    // refresh table view data here
}
于 2016-11-22T00:59:29.200 回答
-11

没有 UITableViewController 的 UIRefreshControl

或者您可以使用UITableViewController而不是UIViewController.

于 2013-11-22T06:46:00.310 回答