在UIViewController内部的UITableView上刷新

时间:2013-11-22 06:44:21

标签: ios uitableview pull-to-refresh uirefreshcontrol

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

11 个答案:

答案 0 :(得分:15)

由于您无法使用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;

希望这有帮助!

答案 1 :(得分: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];
}

答案 2 :(得分: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)

答案 3 :(得分:2)

你可以在这里看到:UIVefControl在UIViewController中(带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

答案 4 :(得分:2)

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

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

答案 5 :(得分: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()
}

答案 6 :(得分:1)

答案 7 :(得分:1)

答案 8 :(得分:0)

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

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

答案 9 :(得分:0)

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

答案 10 :(得分:-11)

UIRefreshControl without UITableViewController

或者您可以使用UITableViewController代替UIViewController