使用UITableView以编程方式创建UINavigationController

使用UITableView以编程方式创建UINavigationController,uitableview,uinavigationcontroller,Uitableview,Uinavigationcontroller,我为这个问题挣扎了很长时间。也许你能帮我。因此,我想用简单的UITableView以编程方式创建一个基本的UINavigationController 例如,UITableView将包含一些有关“颜色”的信息。项目列表应该是“黄色”、“橙色”、“绿色”、“紫色” 如果您能帮我解决这个问题,我将非常高兴。试试这个: @interface MyViewController: UITableViewController { NSArray *data; } @end @implementa

我为这个问题挣扎了很长时间。也许你能帮我。因此,我想用简单的UITableView以编程方式创建一个基本的UINavigationController

例如,UITableView将包含一些有关“颜色”的信息。项目列表应该是“黄色”、“橙色”、“绿色”、“紫色”

如果您能帮我解决这个问题,我将非常高兴。

试试这个:

@interface MyViewController: UITableViewController {
    NSArray *data;
}

@end

@implementation MyViewController

- (id)init
{
    if ((self = [super init]))
    {
        data = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", nil];
    }
    return self;
}

- (void)dealloc
{
    [data release];
    [super dealloc];
}

- (int)tableView:(UITableView *)tv numberOfRowsInSection:(int)s
{
    return [data count];
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip
{
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"CELLID"];
    if (!cell) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:@"CELLID"] autorelease];
    cell.textLabel.text = [data objectAtIndex:ip.row];
    return cell;
}


@end
像这样使用它:

MyViewController *ctrl = [[MyViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:ctrl];
[ctrl release];
等等