如何在 Xcode 中以编程方式更改 UITableViewController 的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11918408/
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
How to change the style of a UITableViewController programmatically in Xcode
提问by Mr. Pie Guy
I'm trying to change the style of a UITableViewController
to grouped. I know you can do this when creating a new table view, but I have a class that extends UITableViewController
, so I don't need to make a new table view. Here's my code:
我正在尝试将 a 的样式更改UITableViewController
为分组。我知道您可以在创建新表视图时执行此操作,但是我有一个扩展类UITableViewController
,因此我不需要创建新表视图。这是我的代码:
#import "DetailViewController.h"
#import "NSArray-NestedArrays.h"
@implementation DetailViewController
@synthesize steak, sectionNames, rowControllers, rowKeys, rowLabels;
- (void)viewDidLoad {
sectionNames = [[NSArray alloc] initWithObjects:[NSNull null], NSLocalizedString(@"General", @"General"), nil];
rowLabels = [[NSArray alloc] initWithObjects:
[NSArray arrayWithObjects:NSLocalizedString(@"Steak Name", @"Steak Name"), nil],
[NSArray arrayWithObjects:NSLocalizedString(@"Steak Wellness", @"Steak Wellness"), NSLocalizedString(@"Steak Type", @"Steak Type"), NSLocalizedString(@"Other", @"Other"), nil]
, nil];
rowKeys = [[NSArray alloc] initWithObjects:
[NSArray arrayWithObjects:@"steakName", nil],
[NSArray arrayWithObjects:@"steakWellness", @"steakType", @"other", nil]
, nil];
// TODO: Populate row controllers array
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sectionNames count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
id theTitle = [sectionNames objectAtIndex:section];
if ([theTitle isKindOfClass:[NSNull class]]) {
return nil;
}
return theTitle;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rowLabels countOfNestedArray:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SteakCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}
NSString *rowKey = [rowKeys nestedObjectAtIndexPath:indexPath];
NSString *rowLabel = [rowLabels nestedObjectAtIndexPath:indexPath];
cell.detailTextLabel.text = rowKey;
cell.textLabel.text = rowLabel;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// TODO: Push editing controller onto the stack
}
@end
采纳答案by Cliff Ribaudo
Not following? What do you mean by you don't have to "make a new table view"?? You still have to instantiate one.
不跟?你不必“制作新的表格视图”是什么意思?你仍然需要实例化一个。
Either you created one already and it has the style you want, or you have to instantiate a new one and set the property on it.
要么您已经创建了一个并且它具有您想要的样式,要么您必须实例化一个新的并在其上设置属性。
tableView.style is READONLY. So you can't change the style of an existing one. You are going to have to do something like:
tableView.style 是只读的。所以你不能改变现有的风格。您将不得不执行以下操作:
[MyTableViewSubClass initWithFrame:aFrame style:UITableViewStyleGrouped];
回答by pengshuai liang
- (instancetype)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
}
return self;
}
回答by Novarg
You can not just change the style of an UITableView
. So you only have 2 options:
你不能只改变一个UITableView
. 所以你只有两个选择:
- Make another
UITableView
which is grouped - Use custom cells
- 制作另一个
UITableView
分组 - 使用自定义单元格
Hope it helps
希望能帮助到你
回答by Freddy
You would take care of this when you instantiated your view controller.
当你实例化你的视图控制器时,你会照顾到这一点。
For example, to instantiate a normal UITableViewController you would do the following.
例如,要实例化一个普通的 UITableViewController,您可以执行以下操作。
UITableViewController *tblCtr = [[UITableViewController alloc]initWithStyle:UITableViewStyleGrouped];
Therefore, if you have extended UITableViewController then your init code should take care of this.
因此,如果您扩展了 UITableViewController,那么您的初始化代码应该处理这个问题。
MyCustomTableViewController *mctvc = [[MyCustomTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
To achieve this you will need to implement this method in your .m file. Below is an example of what your header and implementation file should contain for instantiation.
为此,您需要在 .m 文件中实现此方法。以下是您的头文件和实现文件应包含用于实例化的内容的示例。
Header
标题
@interface MyCustomTableViewController : UITableViewController
{
-(id)initWithStyle:(UITableViewStyle)style;
}
Implementation
执行
@implementation MyCustomTableViewController
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if(self)
{
...
return self;
}
return nil;
}
@end
When you call [super initWithStyle:style] the code provided by apple will take care of building the tableview for you with the requested view style.
当您调用 [super initWithStyle:style] 时,apple 提供的代码将使用请求的视图样式为您构建 tableview。
回答by howeguo
You can create a new tableview to overwrite UITableviewController
's tableview like this:
您可以创建一个新的 tableview 来覆盖这样UITableviewController
的 tableview:
UITableView *tableView = [[UITableView alloc] initWithFrame:self.tableView.frame style:UITableViewStyleGrouped];
self.tableView = tableView;
回答by Bajaj
simply buddy to allocate set the frame and style of table ..example code write down.
简单的哥们分配设置表格的框架和样式..示例代码写下来。
[tableObject initWithFrame:CGRectMake(x,y,width,height) style:UITableViewStyleGrouped];