iphone sdk - UITableView - 无法将表分配给表视图

时间:2009-10-08 11:18:33

标签: iphone uitableview nsarray

这是我的代码的一部分。当我尝试加载包含uitableview的视图时,我的应用程序崩溃了。 我觉得桌子有问题我试着用但却找不到。 请帮忙

    gameTimingTable=[NSArray arrayWithObjects:@"2min + 10sec/coup",@"1min + 15sec/coup",@"5min",nil];

在.h中声明为NSArray *gameTimingTable; 这是我用来将表格分配给uitableview的代码

- (void)viewDidLoad {   

gameTimingTable=[NSArray arrayWithObjects:@"2min + 10sec/coup",@"1min + 15sec/coup",@"5min",nil];



}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // There is only one section.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of time zone names.
    return [gameTimingTable count];
}


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

    static NSString *MyIdentifier = @"MyIdentifier";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil) {
        // Use the default cell style.
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }

    // Set up the cell.
    NSString *cadence = [gameTimingTable objectAtIndex:indexPath.row];
    cell.textLabel.text = cadence;

    return cell;
}

/*
 To conform to Human Interface Guildelines, since selecting a row would have no effect (such as navigation), make sure that rows cannot be selected.
 */
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

非常感谢

1 个答案:

答案 0 :(得分:0)

这里的问题可能是两件事中的一件(或两件事):

1 ...您将从willSelectRowAtIndexPath方法返回nil。如果您没有用户能够点击单元格,只需不要覆盖此方法,即根本不要触摸它。除此之外,您还可以在cellForRowAtIndexPath方法中执行以下操作:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

确保用户点击它时,单元格甚至不会突出显示。

2 ...初始化数组gameTimingTable的方式意味着它在您创建它之后会自动释放,因此无法在代码中的其他地方访问它。使用以下任一方法初始化它:

gameTimingTable=[[NSArray arrayWithObjects:@"2min + 10sec/coup",@"1min + 15sec/coup",@"5min",nil] retain];

// OR ...

 gameTimingTable=[[NSArray alloc] initWithObjects:@"2min + 10sec/coup",@"1min + 15sec/coup",@"5min",nil];

...但请记住在dealloc方法中释放数组:

- (void)dealloc {
[gameTimingTable release];
[super dealloc];

}