【问题标题】:Pull to refresh on UITableView Inside a UIViewController在 UIViewController 内的 UITableView 上拉动以刷新
【发布时间】:2013-12-06 23:16:08
【问题描述】:

我正在尝试在我的应用程序中实现拉动刷新功能。该体系结构使得UITableViewUIViewController 内有一个UITableView。我希望能够在下拉时刷新 tableview。我在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];

【问题讨论】:

标签: ios uitableview pull-to-refresh uirefreshcontrol


【解决方案1】:

UIRefreshControl without UITableViewController

或者您可以使用UITableViewController 代替UIViewController

【讨论】:

【解决方案2】:
【解决方案3】:
【解决方案4】:

由于您不能使用UITableViewController 代替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;

希望这会有所帮助!

【讨论】:

    【解决方案5】:

    你可以在这里看到: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

    【讨论】:

      【解决方案6】:
      -(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];
      }
      

      【讨论】:

      • 你能补充一些解释吗?
      【解决方案7】:

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

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

      【讨论】:

        【解决方案8】:

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

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

        【讨论】:

          【解决方案9】:

          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)
          

          【讨论】:

          • 添加 refreshControll 作为 tableview 子视图也适用于目标 c
          【解决方案10】:

          对于 Swift 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
          }
          

          【讨论】:

            【解决方案11】:

            对于 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()
            }
            

            【讨论】:

              猜你喜欢
              • 2021-06-24
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-05-28
              • 2014-10-23
              • 2013-03-23
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多