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

时间:2022-08-25 07:51:58

I've created a UITableView in Interface Builder using storyboards. The UITableView is setup with static cells and a number of different sections.

我使用故事板在接口构建器中创建了一个UITableView。UITableView是用静态单元格和一些不同的部分设置的。

The issue I'm having is that I'm trying to setup my app in several different languages. To do this I need to be able to change the UITableView section titles somehow.

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

Please can someone help me out? Ideally I'd like to approach the issue using IBOutlets however I suspect this isn't even possible in this case. Any advice and suggestions would be really appreciated.

有人能帮我一下吗?理想情况下,我希望使用iboutlet来处理这个问题,但是我怀疑在这种情况下这是不可能的。如有任何建议和建议,我们将不胜感激。

Thanks in advance.

提前谢谢。

8 个解决方案

#1


257  

Once you have connected your UITableView delegate and datasource to your controller, you could do something like this:

一旦你将UITableView委托和数据源连接到你的控制器,你可以这样做:

- (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;
}

#2


11  

If you are writing code in Swift it would look as an example like this

如果您正在用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"
    }
}

#3


9  

Use the UITableViewDataSource method

使用需要方法

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

#4


5  

titleForHeaderInSection is a delegate method of UITableView so to apply header text of section write as follows,

titleForHeaderInSection是UITableView的一个委托方法,因此要应用section write的标题文本,如下所示,

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

#5


2  

Note that -(NSString *)tableView: titleForHeaderInSection: is not called by UITableView if - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section is implemented in delegate of UITableView;

注意-(NSString *)tableView: titleForHeaderInSection:不被UITableView调用(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)部分在UITableView的delegate中实现;

#6


2  

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 45.0f; 
//set height according to row or section , whatever you want to do!
}

section label text are set.

设置section标签文本。

- (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;
}

#7


2  

Nothing wrong with the other answers but this one offers a non-programmatic solution that may be useful in situations where one has a small static table. The benefit is that one can organize the localizations using the storyboard. One may continue to export localizations from Xcode via XLIFF files. Xcode 9 also has several new tools to make localizations easier.

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

(original)

(原始)

I had a similar requirement. I had a static table with static cells in my Main.storyboard(Base). To localize section titles using .string files e.g. Main.strings(German) just select the section in storyboard and note the Object ID

我也有类似的要求。我在Main.storyboard(Base)中有一个静态单元的静态表格。要使用.string文件本地化节标题,例如.string(德语),只需选择故事板中的节并注意对象ID

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

Afterwards go to your string file, in my case Main.strings(German) and insert the translation like:

然后转到您的字符串文件,在我的例子中是Main.strings(德语),并插入如下翻译:

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

Additional Resources:

#8


0  

I don't know about past versions of UITableView protocols, but as of iOS 9, func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? is part of the UITableViewDataSource protocol.

我不知道UITableView协议的过去版本,但是在ios9, func tableView(tableView: UITableView, titleForHeaderInSection: Int) ->字符串?是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"
      }
   }

You don't need to declare the delegate to fill your table with data.

您不需要声明委托来填充表中的数据。

#1


257  

Once you have connected your UITableView delegate and datasource to your controller, you could do something like this:

一旦你将UITableView委托和数据源连接到你的控制器,你可以这样做:

- (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;
}

#2


11  

If you are writing code in Swift it would look as an example like this

如果您正在用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"
    }
}

#3


9  

Use the UITableViewDataSource method

使用需要方法

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

#4


5  

titleForHeaderInSection is a delegate method of UITableView so to apply header text of section write as follows,

titleForHeaderInSection是UITableView的一个委托方法,因此要应用section write的标题文本,如下所示,

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

#5


2  

Note that -(NSString *)tableView: titleForHeaderInSection: is not called by UITableView if - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section is implemented in delegate of UITableView;

注意-(NSString *)tableView: titleForHeaderInSection:不被UITableView调用(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)部分在UITableView的delegate中实现;

#6


2  

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 45.0f; 
//set height according to row or section , whatever you want to do!
}

section label text are set.

设置section标签文本。

- (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;
}

#7


2  

Nothing wrong with the other answers but this one offers a non-programmatic solution that may be useful in situations where one has a small static table. The benefit is that one can organize the localizations using the storyboard. One may continue to export localizations from Xcode via XLIFF files. Xcode 9 also has several new tools to make localizations easier.

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

(original)

(原始)

I had a similar requirement. I had a static table with static cells in my Main.storyboard(Base). To localize section titles using .string files e.g. Main.strings(German) just select the section in storyboard and note the Object ID

我也有类似的要求。我在Main.storyboard(Base)中有一个静态单元的静态表格。要使用.string文件本地化节标题,例如.string(德语),只需选择故事板中的节并注意对象ID

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

Afterwards go to your string file, in my case Main.strings(German) and insert the translation like:

然后转到您的字符串文件,在我的例子中是Main.strings(德语),并插入如下翻译:

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

Additional Resources:

#8


0  

I don't know about past versions of UITableView protocols, but as of iOS 9, func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? is part of the UITableViewDataSource protocol.

我不知道UITableView协议的过去版本,但是在ios9, func tableView(tableView: UITableView, titleForHeaderInSection: Int) ->字符串?是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"
      }
   }

You don't need to declare the delegate to fill your table with data.

您不需要声明委托来填充表中的数据。