ios 在不重新加载整个表格视图的情况下更改 UITableView 的部分页眉/页脚标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4346944/
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
Changing UITableView's section header/footer title without reloading the whole table view
提问by Ali
Is there any way to reload the section header/footer of a table view without calling
[tableView reloadData];
?
有没有办法在不调用的情况下重新加载表视图的部分页眉/页脚
[tableView reloadData];
?
In fact, I want to show the number of cells in a table view's section in its section footer. The table view is editable and I delete or insert rows using
事实上,我想在其部分页脚中显示表视图部分中的单元格数量。表视图是可编辑的,我删除或插入行使用
– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:
It seems that these methods do not update the section footer. Strangely, when I call these methods the table view data-source method
似乎这些方法不会更新节页脚。奇怪的是,当我调用这些方法时,表视图数据源方法
- (NSString *)tableView:(UITableView *)table titleForFooterInSection:(NSInteger)section
is called (twice!) but it does not update the table view with the new values!!
被调用(两次!)但它不会用新值更新表视图!!
Anyone has any idea how to fix this problem?
任何人都知道如何解决这个问题?
采纳答案by Ali
I managed to do it in an indirect way: I created a UILabel and set it as section header/footer.
我设法以间接方式做到了:我创建了一个 UILabel 并将其设置为节页眉/页脚。
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
// update sectionFooterView.text
return sectionFooterView;
}
- (void)viewDidLoad {
// create sectionFooterView in Interface Builder, change the frame here
// use Font-size:15 , BG-Color: clearColor , text-Color: RGB=(97,105,118)
// and Text-alignment:Center to look like table view's original footer
sectionFooterView.frame = CGRectMake(0, 10, 320, 12);
}
Does anyone know a way to do this without setting a custom footer view?
有没有人知道不设置自定义页脚视图的方法?
回答by Nico teWinkel
If you just have a basic text header or footer, you can access them directly:
如果您只有一个基本的文本页眉或页脚,您可以直接访问它们:
[tableView footerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForFooterInSection:indexPath.section];
similarly for the header:
标题类似:
[tableView headerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForHeaderInSection:indexPath.section];
回答by Craig Miller
This should work:
这应该有效:
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
if let containerView = tableView.footerViewForSection(0) {
containerView.textLabel!.text = "New footer text!"
containerView.sizeToFit()
}
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
回答by Daniel Tovesson
This is how you do it in Swift 3.0
这就是你在 Swift 3.0 中的做法
tableView.beginUpdates()
tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text"
tableView.endUpdates()
回答by hyperspasm
Here's another way to do it:
这是另一种方法:
UITableViewHeaderFooterView *headerView=[self.tableView headerViewForSection:1];
CATransition *animation = [CATransition animation];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.type = kCATransitionFade;
animation.duration = 0.35;
[headerView.textLabel.layer addAnimation:animation forKey:@"kCATransitionFade"];
headerView.textLabel.text=[self tableView:self.tableView titleForHeaderInSection:1];
[headerView.textLabel sizeToFit];
回答by Tim
Check the documentation for UITableView, or more specifically
-reloadSections:withRowAnimation:
检查 UITableView 的文档,或更具体地说
-reloadSections:withRowAnimation:
回答by Loz
Here's a UITableView extension that's a handy shortcut for refreshing the tableView header/footer titles, based on some of the answers above (note the artificial uppercasing of the header title).
这是一个 UITableView 扩展,它是刷新 tableView 页眉/页脚标题的便捷快捷方式,基于上面的一些答案(注意标题标题的人工大写)。
extension UITableView {
func refreshHeaderTitle(inSection section: Int) {
UIView.setAnimationsEnabled(false)
beginUpdates()
let headerView = self.headerView(forSection: section)
headerView?.textLabel?.text = self.dataSource?.tableView?(self, titleForHeaderInSection: section)?.uppercased()
headerView?.sizeToFit()
endUpdates()
UIView.setAnimationsEnabled(true)
}
func refreshFooterTitle(inSection section: Int) {
UIView.setAnimationsEnabled(false)
beginUpdates()
let footerView = self.footerView(forSection: section)
footerView?.textLabel?.text = self.dataSource?.tableView?(self, titleForFooterInSection: section)
footerView?.sizeToFit()
endUpdates()
UIView.setAnimationsEnabled(true)
}
func refreshAllHeaderAndFooterTitles() {
for section in 0..<self.numberOfSections {
refreshHeaderTitle(inSection: section)
refreshFooterTitle(inSection: section)
}
}
}
回答by andre
It worked for me to just reload the section with reloadSections
on my table view
reloadSections
在我的表格视图上重新加载该部分对我有用
回答by Adam Golczak
You can also do it this way
你也可以这样做
override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
switch section {
case 0:
return "Some string"
default:
return ""
}
}
回答by mukaissi
I have found that the footer text doesn't update correctly if the text changes from filling one line to multiple ones back to one line. Reseting the label width and height would solve this issue.
我发现如果文本从填充一行变为多行再回到一行,则页脚文本不会正确更新。重置标签宽度和高度可以解决这个问题。
UIView.setAnimationsEnabled(false)
textLabel.frame = CGRect(x: textLabel.frame.minX,
y: textLabel.frame.minY,
width: 0,
height: 0)
tableView.beginUpdates()
textLabel.text = "Your text"
textLabel.sizeToFit()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)