ios 仅刷新 UITableView 中的自定义标题视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23501952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh only the custom header views in a UITableView?
提问by BlueBear
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?
我的每个部分UITableView
都有自定义UIView
标题。我只需要刷新标题而不是该部分中的其他内容。我已经尝试过了[self.tableView headerViewForSection:i]
,即使它应该返回任何东西,它也不会返回任何东西。无论如何,我可以做到这一点吗?
Edit: Code based around new suggestion
编辑:基于新建议的代码
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.
我也试过了,它会在该方法中调用/更新 UIView,但更改不会在视觉上传播到屏幕上。
for (int i = 0; i < self.objects.count; i++) {
UIView *headerView = [self tableView:self.tableView viewForHeaderInSection:i];
[headerView setNeedsDisplay];
}
回答by Matthias Bauch
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.
不是调用setNeedsDisplay
,而是通过设置它的属性来自己配置标头。当然,您必须获取表中的实际标题,不要调用委托方法,因为该方法通常会创建一个新的标题视图。
I usually do this in a little helper method that is called from tableView:viewForHeaderInSection:
as well.
我通常在一个也被调用的小助手方法中执行此操作tableView:viewForHeaderInSection:
。
e.g.:
例如:
- (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];
}
}
回答by ashack
Create your own subclass of UIView for the headerView, and add a couple of methods to it so your view controller can send whatever UI updates you want to it.
为 headerView 创建您自己的 UIView 子类,并向其添加几个方法,以便您的视图控制器可以向它发送您想要的任何 UI 更新。
回答by Patrick Goley
Calling setNeedsDisplay
only serves to trigger the drawRect:
method of the receiving view. Unless this is where your text color logic is (that is, in the drawRect method of your UIView subclass that you are using for header views), nothing is likely to change. What you need to do is directly call something on the header views to update their state based on your new or changed data or directly access the label in the header view and change it yourself while iterating through them. setNeedsDisplay
will only trigger code found in the drawRect:
method of the view.
调用setNeedsDisplay
仅用于触发drawRect:
接收视图的方法。除非这是您的文本颜色逻辑所在的位置(即,在您用于标题视图的 UIView 子类的 drawRect 方法中),否则不会有任何更改。您需要做的是直接调用标题视图上的某些内容以根据您的新数据或更改的数据更新它们的状态,或者直接访问标题视图中的标签并在迭代它们时自己更改它。setNeedsDisplay
只会触发drawRect:
在视图的方法中找到的代码。
回答by Jef
I recently had this exact problem and only overcame it by keeping a pointer to the headers as they were created. I did it in Swift and with a swift only dictionary type but the idea could be massaged into objC somehow.
我最近遇到了这个确切的问题,只有通过在创建标题时保留指向标题的指针来克服它。我在 Swift 中完成了它,并且只使用了一个 swift 字典类型,但这个想法可以以某种方式融入到 objC 中。
var headers : [ Int : UIView ] = [ : ] //dictionary for keeping pointer to headers
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if let h = super.tableView(tableView , viewForHeaderInSection: section) { //or create your view (this viewController inherits it)..
self.headers.updateValue(h , forKey: section) //keep a pointer to it before you return it..
return h
}
return nil
}
回答by Jacob Barnard
Swift 5Solution:
斯威夫特 5解决方案:
Delegate method of your table view:
表视图的委托方法:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// Ideally, dequeue your header view...
var view: UITableViewHeaderFooterView?
if section == 0 { view = ... // dequeue appropriately
} else if section == 1 { view = ... // dequeue appropriately
} else ... // etc.
setupHeaderView(&view)
return view
}
In the area of code where you want to do the header view update:
在要进行标题视图更新的代码区域中:
var headerView = tableView.headerView(forSection: i)
setupHeaderView(&headerView)
Header setup method:
标题设置方法:
func setupHeaderView(_ view: inout UITableViewHeaderFooterView?) {
// do something with `view` here
}
回答by Diaa SAlAm
tableView.reloadSections([0,0], with: .automatic)
https://developer.apple.com/documentation/uikit/uitableview/1614954-reloadsections
https://developer.apple.com/documentation/uikit/uitableview/1614954-reloadsections
回答by Stonz2
When you create your custom header views, use UITableViewHeaderFooterView
instead of a simple UIView
and when you initialize it, use something like this:
创建自定义标题视图时,请使用UITableViewHeaderFooterView
而不是简单的,UIView
并在初始化时使用如下内容:
UITableViewHeaderFooterView *cell = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"reuseIdentifier"];
The reuseIdentifier
will tell your UITableView
to keep track of the header and it should no longer return null
when you try to access it with [self.tableView headerViewForSection:i]
该reuseIdentifier
会告诉你UITableView
跟踪头的,它不应该再返回null
当您尝试访问它[self.tableView headerViewForSection:i]
You should then be able to use setNeedsDisplay
on the UITableViewHeaderFooterView
you get back from headerViewForSection
after making your modifications.
然后,您应该能够使用setNeedsDisplay
在UITableViewHeaderFooterView
你回来headerViewForSection
做出修改之后。