2013-11-22 40 views
9

我想在我的應用程序中實現拉來刷新功能。該架構是這樣的,在UIViewController內部有一個UITableView。我希望能夠刷新桌面視圖。我在viewDidLoad方法中嘗試了下面的代碼,但它不起作用。任何人都可以告訴我我在哪裏執行錯了嗎?拉UITableView在UIViewController裏面刷新

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]; 
+1

可能重複http://stackoverflow.com/questions/12497940/uirefreshcontrol-without-uitableviewcontroller) – Amar

+0

[拉取UITableViewController時刷新UITableView]的可能重複(http://stackoverflow.com/questions/10291537/pull-to-re fresh-uitableview-without-uitableviewcontroller) – Lanorkin

回答

-11

UIRefreshControl without UITableViewController

或者你可以使用UITableViewController代替UIViewController

+0

由於某些限制,不能這樣做。有沒有一種解決方法,我不需要改變架構。 –

+0

Amar在您的問題上方發佈良好鏈接。 http://stackoverflow.com/questions/12497940/uirefreshcontrol-without-uitableviewcontroller –

15

既然你不能使用的,而不是UITableViewControllerUIViewController,試試這樣做:

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; 

希望這有助於!

2

,你可以在這裏看到:UIRefreshControl在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

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

你可以添加一些解釋嗎? – Sal00m

3

爲雨燕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) 
+0

添加refreshControll作爲tableview子視圖也在目標c上工作 –

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 
} 
1

爲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() 
} 
[UIRefreshControl而不的UITableViewController](的