添加TableView子视图崩溃应用程序

时间:2011-08-10 07:33:17

标签: iphone ipad

我正在尝试添加一个tableview,所以当有人按下某个按钮时,该视图会切换到tableview,并提供一些选择。

以下是我的按钮代码:

-(IBAction)buttonPressed:(id)sender  
    {
         LevelChoice *level = [[LevelChoice alloc] initWithNibName:nil bundle:nil];
         [self.view addSubview:level.view];
         [level release];   
   }

以下是我的UITableViewController子类的代码片段:

LevelChoice.h 代码:

@interface LevelChoice : UITableViewController {
    NSArray *choices; 
}

LevelChoice.m

代码:

-(void)viewDidLoad
{ 
    choices = [[NSArray alloc] initWithObjects:@"Level 1", @"Level 2", @"Level 3", nil];

    [super viewDidLoad];

}

代码:

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

     }
  cell.text = [choices objectAtIndex:indexPath.row];
  return cell;

}

有谁知道我错过了什么?

1 个答案:

答案 0 :(得分:1)

区分控制器和视图。你可以

  1. UITableViewControllerpresentModalViewController:animated:pushViewController:animated:一起展示。 (是的,在这种情况下,你可以释放它。)
  2. 或者只在现有视图控制器中保留UITableView,并根据需要使用hidden属性显示或隐藏它。当然,您需要为表格实现datasourcedelegate方法。
相关问题