仅刷新UITableView中的自定义标头视图? [英] Refresh only the custom header views in a UITableView?

查看:93
本文介绍了仅刷新UITableView中的自定义标头视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UITableView在每个部分中都有自定义的UIView标头.我只需要刷新标题,而不刷新本节中的其他内容.我已经尝试过[self.tableView headerViewForSection:i],即使应该也不会返回任何内容.无论如何,我能做到这一点吗?

My UITableView has custom UIView headers in every section. I am needing to refresh only the headers and not the other content in the section. I have tried out [self.tableView headerViewForSection:i] and it does not return anything even though it should. Is there anyway that I can do this?

基于新建议的代码

我也给了这个镜头,它在该方法中调用/更新了UIView,但是更改不会在视觉上传播到屏幕上.

I have given this a shot as well and it calls/updates the UIView within that method, but the changes do not visually propagate onto the screen.

for (int i = 0; i < self.objects.count; i++) {
    UIView *headerView = [self tableView:self.tableView viewForHeaderInSection:i];
    [headerView setNeedsDisplay];
}

推荐答案

您可以通过设置标题的属性来自行配置标题,而不是调用setNeedsDisplay.当然,您必须在表中获取实际的标题,不要调用委托方法,因为该方法通常会创建一个新的标题视图.

Instead of calling setNeedsDisplay, configure the header yourself by setting it's properties. And of course you have to get the actual headers in the table, don't call the delegate method, because that method usually creates a new header view.

我通常也使用tableView:viewForHeaderInSection:调用的小助手方法来执行此操作.

I usually do this in a little helper method that is called from tableView:viewForHeaderInSection: as well.

例如:

- (void)configureHeader:(UITableViewHeaderFooterView *)header forSection:(NSInteger)section {
    // configure your header
    header.textLabel.text = ...
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"Header"];
    [self configureHeader:header forSection:section];
}

- (void)reloadHeaders {
    for (NSInteger i = 0; i < [self numberOfSectionsInTableView:self.tableView]; i++) {
        UITableViewHeaderFooterView *header = [self.tableView headerViewForSection:i];
        [self configureHeader:header forSection:i];
    }
}

这篇关于仅刷新UITableView中的自定义标头视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆