Uitableviewcell没有加载

时间:2011-05-08 02:12:02

标签: objective-c cocoa-touch xcode

我遇到了一个我无法弄清楚的问题。当我运行我的应用程序并单击其中一个表格视图单元格时,单元格未加载与其关联的视图控制器。

以下是.m代码的副本

#import "FirstViewController.h"
#import "arraymap.h"
#import "crackedmap.h"
#import "crisismap.h"
#import "firingrangemap.h"

@implementation FirstViewController
@synthesize imageArray, cellTextArray;

- (void)viewDidLoad
{
    self.title = @"Maps";
    imageArray = [[NSArray alloc] initWithObjects:@"1.png", @"1.png", @"1.png", @"1.png", nil];
    cellTextArray = [[NSArray alloc] initWithObjects:@"cell1", @"cell2", @"cell3", @"cell4", nil];
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

// Customize the appearance of table view cells.
- (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.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]]] autorelease];

    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 40, cell.frame.size.width-20, 35)];
    [lab setBackgroundColor:[UIColor clearColor]];
    [lab setText:[cellTextArray objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:[lab autorelease]];

    // Configure the cell.
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 {
 if (editingStyle == UITableViewCellEditingStyleDelete)
 {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }
 else if (editingStyle == UITableViewCellEditingStyleInsert)
 {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *viewController = nil;
    switch (indexPath.row) {
        case 0:
            viewController = [[arraymap alloc] initWithNibName:@"arraymap" bundle:nil];
            break;
        case 1:
            viewController = [[crackedmap alloc] initWithNibName:@"crackedmap" bundle:nil];
            break;
        case 2:
            viewController = [[crisismap alloc] initWithNibName:@"crisismap" bundle:nil];
            break;
        case 3:
            viewController = [[firingrangemap alloc] initWithNibName:@"firingrangemap" bundle:nil];
            break;
        default:
            break;
    }
    [viewController setTitle:[NSString stringWithFormat:@"%i", indexPath.row+1]];
    [self.navigationController pushViewController:[viewController autorelease] animated:YES];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

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

@end

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加日志记录调用以确保viewController不是nil?先检查一下,如果没有,你知道你的开关不正确。