如何在我的应用程序中重新创建“设置应用程序”类似的tableview?

时间:2021-03-23 07:33:43

I want to add a view to my application that allows the user to change several (many) settings. It should look like the built in "Settings Application" (see here) but it has to be within my app itself. At the moment I just have a tableview and I manually add different cell objects (corresponding to switches, sliders, etc). Is there a smarter of doing that? I have the feeling that I'm reinventing the wheel.

我想在我的应用程序中添加一个视图,允许用户更改多个(多个)设置。它应该看起来像内置的“设置应用程序”(见这里),但它必须在我的应用程序本身。目前我只有一个tableview,我手动添加不同的单元格对象(对应于开关,滑块等)。有更聪明的做法吗?我觉得我正在重新发明*。

4 个解决方案

#1


4  

No, that's the way, returning individual cells in the tableView:cellForRowAtIndexPath: method. Just remember that a UITableViewCell has useful properties such an accessory mark (a detail discolsure button, a disclosure indicator or a checkmark) and an image at the left side.

不,就是这样,返回tableView:cellForRowAtIndexPath:方法中的单个单元格。请记住,UITableViewCell具有有用的属性,如附件标记(细节变色按钮,公开指示符或复选标记)和左侧的图像。

#2


11  

This is an old question by internet standards, but I've found an amazing way to do this that requires very little effort. I was pulling my hair out trying to build a 2nd table view in another nib before i found these guys: InAppSettingsKit

这是一个基于互联网标准的老问题,但我发现了一个很棒的方法,这需要很少的努力。在我找到这些家伙之前,我正试着在另一个笔尖上构建第二个表格视图:InAppSettingsKit

Basically they've got all the views and the backend stuff done, all you do is add the files to your project and implement a few delegate methods in the class you're calling the settings page from (along with a button in your UI to open the settings panel, etc), and it takes care of the NSUserDefaults stuff. The UI and the settings values and keys are all generated from the same Settings.bundle used for the real settings app, which means your settings can be altered by the user in both places, in the app AND in the settings app. I found this much easier than messing with the delegate methods for a custom table view, and now i don't actually have to choose between Settings.app and something in-app because it does both.

基本上他们已经完成了所有视图和后端工作,您只需将文件添加到项目中,并在您调用设置页面的类中实现一些委托方法(以及UI中的按钮)打开设置面板等),它会处理NSUserDefaults的内容。 UI和设置值和键都是从用于真实设置应用程序的相同Settings.bundle生成的,这意味着用户可以在应用程序和设置应用程序中的两个位置更改您的设置。我发现这比使用自定义表视图的委托方法更容易,现在我实际上不必在Settings.app和应用程序之间进行选择,因为它同时执行这两种操作。

All that's left is to pull the values you need for each settings key later on in your app when you need them, with [[NSUserDefaults standardUserDefaults] objectForKey:@"key"].

剩下的就是在您需要的时候在应用程序中为每个设置键提取所需的值,使用[[NSUserDefaults standardUserDefaults] objectForKey:@“key”]。

#3


4  

Here's some code I used to set up a preferences page:

这是我用来设置首选项页面的一些代码:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case (0):
            return 3;
            break;
        case (1):
            return 2;
            break;
        case (2):
            return 1;
            break;
    }
    return 0;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case (0):
            return @"Numbers";
            break;
        case (1):
            return @"Randomize";
            break;
        case (2):
            return @"About";
            break;
    }
    return nil;
}

The switch statement sets up 3 sections: Numbers, Randomize, and Versions. You can fill in the sections with whatever controls you need

switch语句设置3个部分:Numbers,Randomize和Versions。您可以使用您需要的任何控件填写这些部分

#4


0  

If you're really gung ho, you can simply use the same format Apple uses to describe settings that would normally go into the settings bundle - but instead parse them yourself, and dynamically build a table based on the settings types.

如果你真的很好,你可以简单地使用Apple用来描述通常进入设置包的设置的相同格式 - 而是自己解析它们,并根据设置类型动态构建表。

It's a lot of work, but then again half the work has already been done for you as Apple has a very well though out plist schema for how you define settings.

这是很多工作,但是再一半的工作已经为你完成了,因为Apple有一个很好的尽管plist架构如何定义设置。

That also gives you the advantage of extending the settings format to include some extra abilities the Settings app would not have...

这也为您提供了扩展设置格式以包含一些设置应用程序不具备的额外功能的优势...

#1


4  

No, that's the way, returning individual cells in the tableView:cellForRowAtIndexPath: method. Just remember that a UITableViewCell has useful properties such an accessory mark (a detail discolsure button, a disclosure indicator or a checkmark) and an image at the left side.

不,就是这样,返回tableView:cellForRowAtIndexPath:方法中的单个单元格。请记住,UITableViewCell具有有用的属性,如附件标记(细节变色按钮,公开指示符或复选标记)和左侧的图像。

#2


11  

This is an old question by internet standards, but I've found an amazing way to do this that requires very little effort. I was pulling my hair out trying to build a 2nd table view in another nib before i found these guys: InAppSettingsKit

这是一个基于互联网标准的老问题,但我发现了一个很棒的方法,这需要很少的努力。在我找到这些家伙之前,我正试着在另一个笔尖上构建第二个表格视图:InAppSettingsKit

Basically they've got all the views and the backend stuff done, all you do is add the files to your project and implement a few delegate methods in the class you're calling the settings page from (along with a button in your UI to open the settings panel, etc), and it takes care of the NSUserDefaults stuff. The UI and the settings values and keys are all generated from the same Settings.bundle used for the real settings app, which means your settings can be altered by the user in both places, in the app AND in the settings app. I found this much easier than messing with the delegate methods for a custom table view, and now i don't actually have to choose between Settings.app and something in-app because it does both.

基本上他们已经完成了所有视图和后端工作,您只需将文件添加到项目中,并在您调用设置页面的类中实现一些委托方法(以及UI中的按钮)打开设置面板等),它会处理NSUserDefaults的内容。 UI和设置值和键都是从用于真实设置应用程序的相同Settings.bundle生成的,这意味着用户可以在应用程序和设置应用程序中的两个位置更改您的设置。我发现这比使用自定义表视图的委托方法更容易,现在我实际上不必在Settings.app和应用程序之间进行选择,因为它同时执行这两种操作。

All that's left is to pull the values you need for each settings key later on in your app when you need them, with [[NSUserDefaults standardUserDefaults] objectForKey:@"key"].

剩下的就是在您需要的时候在应用程序中为每个设置键提取所需的值,使用[[NSUserDefaults standardUserDefaults] objectForKey:@“key”]。

#3


4  

Here's some code I used to set up a preferences page:

这是我用来设置首选项页面的一些代码:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case (0):
            return 3;
            break;
        case (1):
            return 2;
            break;
        case (2):
            return 1;
            break;
    }
    return 0;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case (0):
            return @"Numbers";
            break;
        case (1):
            return @"Randomize";
            break;
        case (2):
            return @"About";
            break;
    }
    return nil;
}

The switch statement sets up 3 sections: Numbers, Randomize, and Versions. You can fill in the sections with whatever controls you need

switch语句设置3个部分:Numbers,Randomize和Versions。您可以使用您需要的任何控件填写这些部分

#4


0  

If you're really gung ho, you can simply use the same format Apple uses to describe settings that would normally go into the settings bundle - but instead parse them yourself, and dynamically build a table based on the settings types.

如果你真的很好,你可以简单地使用Apple用来描述通常进入设置包的设置的相同格式 - 而是自己解析它们,并根据设置类型动态构建表。

It's a lot of work, but then again half the work has already been done for you as Apple has a very well though out plist schema for how you define settings.

这是很多工作,但是再一半的工作已经为你完成了,因为Apple有一个很好的尽管plist架构如何定义设置。

That also gives you the advantage of extending the settings format to include some extra abilities the Settings app would not have...

这也为您提供了扩展设置格式以包含一些设置应用程序不具备的额外功能的优势...