Подтвердить что ты не робот

UITableView и UIRefreshControl

Я пытаюсь переместить UIRefreshControl поверх моего headerView или, по крайней мере, заставить его работать с contentInset. Кто-нибудь знает, как его использовать?

Я использовал заголовок, чтобы иметь хороший фон при прокрутке в TableView. Я хотел иметь прокручиваемый фон.

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Set up the edit and add buttons.

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.backgroundColor = [UIColor clearColor];

[self setWantsFullScreenLayout:YES];

self.tableView.contentInset = UIEdgeInsetsMake(-420, 0, -420, 0);

UIImageView *top = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top.jpg"]];
self.tableView.tableHeaderView = top;

UIImageView *bottom = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottom.jpg"]];
self.tableView.tableFooterView = bottom;

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"settingsIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)];
self.navigationItem.leftBarButtonItem = leftButton;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addList)];
self.navigationItem.rightBarButtonItem = addButton;

//Refresh Controls
self.refreshControl = [[UIRefreshControl alloc] init];

[self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged];
}
4b9b3361

Ответ 1

Я не совсем уверен, что вы намерены делать с содержимым ContentInset, но с точки зрения добавления UIRefreshControl в UITableView это можно сделать. UIRefreshControl на самом деле предназначен для использования с UITableViewController, но если вы просто добавляете его в качестве подзадачи в UITableView, это волшебным образом работает. Обратите внимание, что это недокументированное поведение и может не поддерживаться в другой версии iOS, но оно является законным, поскольку оно не использует частные API.

- (void)viewDidLoad
{
    ...
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.myTableView addSubview:refreshControl];
}

- (void)handleRefresh:(id)sender
{
    // do your refresh here...
}

Кредитуется @Keller за то, что заметили это.

Ответ 2

@Ответ на Echelon - это место, но у меня есть одно небольшое предложение. Добавьте элемент управления обновлением как @property, чтобы позднее вы могли получить к нему доступ.

в YourViewController.h

@property (nonatomic, strong) UIRefreshControl *refreshControl;

в YourViewController.m

-(void) viewDidLoad {
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:self.refreshControl]; //assumes tableView is @property
}

И большая причина сделать это таким образом...

-(void)refresh {
    [self doSomeTask]; //calls [taskDone] when finished
}

-(void)taskDone {
    [self.refreshControl endRefreshing];
}

Просто дает вам доступ к общему доступу к UIRefreshControl, чтобы вы могли endRefreshing или проверить свойство isRefreshing для UIRefreshControl.