在iphone上创建分组表视图控制器的最佳方法是什么?

时间:2021-04-24 07:38:01

UITableViewController has an initWithStyle method where you pass it the style you want the table view to use, plain or grouped. These means that to create and use my table view controller, somewhere else in the code, possibly in multiple places, I do this:

UITableViewController有一个initWithStyle方法,您可以在其中传递您希望表视图使用的样式,普通或分组。这意味着要创建和使用我的表视图控制器,代码中的其他位置,可能在多个地方,我这样做:

MyViewController *myViewController = [[MyViewController alloc] initWithStyle: UITableViewStyleGrouped];
[self.navigationController pushViewController: myViewController animated: YES];
[myViewController release];

These seems backwards to me. The classes that use my UITableViewController should not know or care if it used grouped or plain style, that is a concern that the MyViewController should take care of. What's the idiomatic way of dealing with this? Override the init method in MyViewController to call initWithStyle?

这些似乎对我不利。使用我的UITableViewController的类不应该知道或关心它是否使用了分组或简单样式,这是MyViewController应该关注的问题。处理这个问题的惯用方法是什么?覆盖MyViewController中的init方法来调用initWithStyle?

3 个解决方案

#1


My answer as a newbie is 'just do it' and worry about the philosophical design issues later. All the examples I've seen, including popular books have that 'pattern' so it can't be too wrong.

我作为新手的回答是“只是去做”,后来担心哲学设计问题。我见过的所有例子,包括流行书都有“模式”,所以它不会太错。

#2


Just be careful when overriding one init method on a superclass that calls another. I'm not sure about it here, but IF initWithStyle allready calls init in the superclass when you override init in the subclass and call the super you might have a loop on your hands.

在调用另一个超类的超类上覆盖一个init方法时要小心。我在这里不确定,但是当你在子类中覆盖init并调用超级时,initWithStyle allready在超类中调用init,你可能会有一个循环。

#3


I would just create a table view controller through xcode, assign the table the grouped style in Interface Builder, then override the init method if necessary:

我只想通过xcode创建一个表视图控制器,在Interface Builder中为表分配分组的样式,然后在必要时覆盖init方法:

- (id)init {
    self = [super initWithNibName:@"MyViewControllerNibFileName" bundle:nil];
    if (self) {
        // Do other customisation
    }
    return self;
}

I needed to explicitly set the nib name for some reason, but sometimes the view controller just finds it automagically (I'm still a newbie, so not sure why this happens)

我需要出于某种原因显式设置nib名称,但有时视图控制器只是自动找到它(我仍然是新手,所以不确定为什么会发生这种情况)

#1


My answer as a newbie is 'just do it' and worry about the philosophical design issues later. All the examples I've seen, including popular books have that 'pattern' so it can't be too wrong.

我作为新手的回答是“只是去做”,后来担心哲学设计问题。我见过的所有例子,包括流行书都有“模式”,所以它不会太错。

#2


Just be careful when overriding one init method on a superclass that calls another. I'm not sure about it here, but IF initWithStyle allready calls init in the superclass when you override init in the subclass and call the super you might have a loop on your hands.

在调用另一个超类的超类上覆盖一个init方法时要小心。我在这里不确定,但是当你在子类中覆盖init并调用超级时,initWithStyle allready在超类中调用init,你可能会有一个循环。

#3


I would just create a table view controller through xcode, assign the table the grouped style in Interface Builder, then override the init method if necessary:

我只想通过xcode创建一个表视图控制器,在Interface Builder中为表分配分组的样式,然后在必要时覆盖init方法:

- (id)init {
    self = [super initWithNibName:@"MyViewControllerNibFileName" bundle:nil];
    if (self) {
        // Do other customisation
    }
    return self;
}

I needed to explicitly set the nib name for some reason, but sometimes the view controller just finds it automagically (I'm still a newbie, so not sure why this happens)

我需要出于某种原因显式设置nib名称,但有时视图控制器只是自动找到它(我仍然是新手,所以不确定为什么会发生这种情况)