2013-11-22 13 views
9

Tôi đang cố gắng triển khai chức năng kéo để làm mới trong ứng dụng của mình. Kiến trúc là như vậy mà có một UITableView bên trong một UIViewController. Tôi muốn có thể làm mới chế độ xem bảng khi kéo xuống. Tôi đã thử các mã dưới đây trong phương pháp viewDidLoad, nhưng nó không hoạt động. Bất kỳ ai có thể cho tôi biết tôi đang sai khi triển khai không?Kéo để làm mới trên UITableView Bên trong một 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

thể trùng lặp của [UIRefreshControl mà không UITableViewController] (http://stackoverflow.com/questions/12497940/uirefreshcontrol-without-uitableviewcontroller) – Amar

+0

có thể trùng lặp của [Kéo để làm mới UITableView mà không có UITableViewController] (http://stackoverflow.com/questions/10291537/pull-to-re fresh-uitableview-without-uitableviewcontroller) – Lanorkin

Trả lời

-11

UIRefreshControl without UITableViewController

Hoặc bạn có thể sử dụng UITableViewController thay vì một UIViewController.

+0

Không thể làm điều đó do một số hạn chế. Có một công việc xung quanh như vậy mà tôi không phải thay đổi kiến ​​trúc. –

+0

Amar đăng liên kết tốt phía trên câu hỏi của bạn. http://stackoverflow.com/questions/12497940/uirefreshcontrol-without-uitableviewcontroller –

15

Vì bạn không thể sử dụng một UITableViewController thay vì UIViewController, hãy thử làm điều này:

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; 

Hope this helps!

2

bạn có thể xem tại đây: UIRefreshControl trong UIViewController (với 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

Bạn có thể thêm một số giải thích? – Sal00m

3

cập nhật câu trả lời cho 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) 
+0

thêm refreshControll dưới dạng xem phụ đề bảng cũng hoạt động trên c mục tiêu –

0

Đối 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

Đối Swift 3 và iOS tương thích ngược

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() 
} 
Các vấn đề liên quan