【问题标题】:UITableView and UIRefreshControlUITableView 和 UIRefreshControl
【发布时间】:2012-09-22 07:08:33
【问题描述】:

我正在尝试将 UIRefreshControl 移动到我的 headerView 之上,或者至少让它与 contentInset 一起使用。有人知道怎么用吗?

在 TableView 中滚动时,我使用 headerView 来获得漂亮的背景。我想要一个可滚动的背景。

- (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];
}

【问题讨论】:

    标签: uitableview uirefreshcontrol


    【解决方案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 for noticing this

    【讨论】:

    • 在一个普通的 UIViewController 中你没有 self.refreshControl,所以你完全错了 lensovet
    • 如果您的 tableview 有标题视图(至少在 iOS7 上),此方法会有一些问题。当您在刷新时向下滚动时,表格部分会完全错位,偏移标题视图的高度。
    【解决方案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 或检查 UIRefreshControl 的 isRefreshing 属性。

    【讨论】:

    • @property (nonatomic,strong) UIRefreshView *refreshControl; 你可能是指UIRefreshControl 这里。对吗?
    • @self.name 为什么完成后会调用taskDone?这让我很惊讶。其实应该执行taskDone再回来刷新,对吗?为您的答案 +1。
    猜你喜欢
    • 2013-09-25
    • 1970-01-01
    • 2015-07-30
    • 2013-02-03
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多