[英]UITableView - Section header. How to change text?
我有一個使用Storyboard的項目。 我有一個帶靜態單元格和組樣式的UITableView。
我需要在一個部分中更改部分文本,具體取決於在分段控件中進行的選擇(在另一部分中)
我找到了一些解決方案,表明你應該使用覆蓋這個方法:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
並通過調用觸發更新:
[[self tableView]reloadSections:[NSIndexSet indexSetWithIndex:SectionToChange] withRowAnimation:UITableViewRowAnimationFade];
問題是當我調用reloadSections時,相關部分中的所有行和單元格都會被刪除。 文本更新正確,但有這種不必要的副作用。
我想我在這里找到了答案: 更改UITableView節標題而不使用tableView:titleForHeaderInSection
它可能不是很優雅,但接縫工作。
我可以通過調用: [self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
只觸發更新到沒有不需要的副作用的節頭[self.tableView headerViewForSection:1].textLabel.text = [self tableView:self.tableView titleForHeaderInSection:1];
所以唯一需要的是實現:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 1){
if (self.segmentX.selectedSegmentIndex == 0) // or some condition
return @"Header one";
else
return @"Header two";
}
return nil;
}
如果您已實現這些功能,那么刪除它們應該可以解決您的問題:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{ if (section == 0)
{
lbl.text = @"abc";
}
if (section == 2)
{
lbl.text = @"2ndsectionbeigns";
}
return headerview;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.