2012-07-04 141 views
0

我一直在为这个问题苦苦挣扎了很长时间。也许你可以帮助我。所以我想用简单的UITableView以编程方式创建一个基本的UINavigationController。用UITableView以编程方式创建UINavigationController

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

如果你能帮助我解决这个问题,我真的很高兴。

回答

0

试试这个:

@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]; 

相关问题