ChatGPT解决这个技术问题 Extra ChatGPT

如何以编程方式设置 UITableView 部分标题(iPhone/iPad)?

我使用 storyboards 在 Interface Builder 中创建了一个 UITableViewUITableView 设置有 static cells 和许多不同的部分。

我遇到的问题是我试图用几种不同的语言设置我的应用程序。为此,我需要能够以某种方式更改 UITableView 部分标题。

请问有人可以帮我吗?理想情况下,我想使用 IBOutlets 来解决这个问题,但我怀疑在这种情况下这甚至是不可能的。任何意见和建议将不胜感激。

提前致谢。


A
Alladinian

将 UITableView delegatedatasource 连接到控制器后,您可以执行以下操作:

对象

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSString *sectionName;
    switch (section) {
        case 0:
            sectionName = NSLocalizedString(@"mySectionName", @"mySectionName");
            break;
        case 1:
            sectionName = NSLocalizedString(@"myOtherSectionName", @"myOtherSectionName");
            break;
        // ...
        default:
            sectionName = @"";
            break;
    }    
    return sectionName;
}

迅速

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    let sectionName: String
    switch section {
        case 0:
            sectionName = NSLocalizedString("mySectionName", comment: "mySectionName")
        case 1:
            sectionName = NSLocalizedString("myOtherSectionName", comment: "myOtherSectionName")
        // ...
        default:
            sectionName = ""
    }
    return sectionName
}

如果您使用静态单元设置故事板,您确定实际上会被调用吗?我似乎没有被调用。
啊,看来您必须实现 numberOfSectionsInTableView:tableView: 才能调用它。
对于静态单元格,(大多数)所有其他数据源方法都没有实现。
@drewish numberOfSectionsInTableView:tableView: 在 IB 中为静态单元实现。
drawish 是正确的 - 如果您实现 numberOfSectionsInTableView: ,则调用 title 方法并覆盖情节提要。由于这是一个静态表格视图,因此可以使用返回常数的方法覆盖它@wcochran
B
BCI

如果您正在使用 Swift 编写代码,它看起来像这样的示例

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
    switch section
    {
        case 0:
            return "Apple Devices"
        case 1:
            return "Samsung Devices"
        default:
            return "Other Devices"
    }
}

g
geraldWilliam

使用 UITableViewDataSource 方法

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

p
pkamb

titleForHeaderInSection 是 UITableView 的委托方法,因此应用部分写入的标题文本如下,

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"Hello World";
}

J
Julian D.

请注意,如果在 UITableView 的委托中实现 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section,则 UITableView 不会调用 -(NSString *)tableView: titleForHeaderInSection:


M
Morgan Wilde

我不知道 UITableView 协议的过去版本,但在 iOS 9 中,func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?UITableViewDataSource 协议的一部分。

   class ViewController: UIViewController {

      @IBOutlet weak var tableView: UITableView!

      override func viewDidLoad() {
         super.viewDidLoad()
         tableView.dataSource = self
      }
   }

   extension ViewController: UITableViewDataSource {
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
         return "Section name"
      }
   }

您无需声明 delegate 即可用数据填充您的表。


Swift 5.3 似乎不需要extension。 Xcode 构建消息:“'SettingsViewController' 从这里的超类继承对协议 'UITableViewDataSource' 的一致性”
M
Manjeet
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 45.0f; 
//set height according to row or section , whatever you want to do!
}

部分标签文本已设置。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionHeaderView;

        sectionHeaderView = [[UIView alloc] initWithFrame:
                             CGRectMake(0, 0, tableView.frame.size.width, 120.0)];


    sectionHeaderView.backgroundColor = kColor(61, 201, 247);

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:
                            CGRectMake(sectionHeaderView.frame.origin.x,sectionHeaderView.frame.origin.y - 44, sectionHeaderView.frame.size.width, sectionHeaderView.frame.size.height)];

    headerLabel.backgroundColor = [UIColor clearColor];
    [headerLabel setTextColor:kColor(255, 255, 255)];
    headerLabel.textAlignment = NSTextAlignmentCenter;
    [headerLabel setFont:kFont(20)];
    [sectionHeaderView addSubview:headerLabel];

    switch (section) {
        case 0:
            headerLabel.text = @"Section 1";
            return sectionHeaderView;
            break;
        case 1:
            headerLabel.text = @"Section 2";
            return sectionHeaderView;
            break;
        case 2:
            headerLabel.text = @"Section 3";
            return sectionHeaderView;
            break;
        default:
            break;
    }

    return sectionHeaderView;
}

T
Tommie C.

其他答案没有错,但这个提供了一种非编程解决方案,在静态表较小的情况下可能很有用。好处是可以使用故事板组织本地化。可以继续通过 XLIFF 文件从 Xcode 导出本地化。 Xcode 9 还提供了几个新工具来简化本地化。

(原来的)

我有类似的要求。我的 Main.storyboard(Base) 中有一个带有静态单元格的静态表。要使用 .string 文件本地化章节标题,例如 Main.strings(German),只需在情节提要中选择章节并记下对象 ID

https://i.stack.imgur.com/15zvb.png

然后转到您的字符串文件,在我的情况下为 Main.strings(German) 并插入翻译,如:

"MLo-jM-tSN.headerTitle" = "Localized section title";

其他资源:

苹果 WWDC 2017

关于本地化的中篇文章 ~ Xcode 9 / Swift 4