如何在xcode中设置UITableViewCell标识符?

时间:2011-04-05 07:16:20

标签: objective-c uitableview exc-bad-access ios-4.2 reuseidentifier

当我在项目中的viewcontroller中的UITableView中选择一个单元格时,我会随机崩溃。

我认为问题是由我试图重复使用的单元格造成的。

这是代码。

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

        [self reinitializeStrArray];

        static NSString *CellIdentifier = @"cell";

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

        cell.textLabel.text = [strArray objectAtIndex:indexPath.row];

        return cell;
}



- (void)addStrTableToSubView{
    strTable = [[UITableView alloc] initWithFrame:CGRectMake(20, 110, 280, 285)];
    [strTable setDelegate:self];
    [strTable setDataSource:self];

    [self.view addSubview:strTable];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 83, 280, 28)] autorelease];
    UIColor *myColor = [UIColor colorWithRed:(215 / 255.0) green:(145 / 255.0) blue:(0.0 / 255.0) alpha: 1];
    label.backgroundColor = myColor;

    label.font = [UIFont boldSystemFontOfSize:24];
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.8];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.text = [@"strings" capitalizedString];
    [self.view  addSubview:label];

    [strTable selectRowAtIndexPath:scrollStrIndexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
}

我在XCode中创建了我的表。所以IB中没有UITableViewCell对象,它有一个标识符。

创建表时是否可以在代码中添加它? 或者是其他地方的问题?

由于

1 个答案:

答案 0 :(得分:1)

当您获得EXC_BAD_ACCESS时,通常意味着内存管理不正确(即对象未正确释放)。我的建议是启用NSZombieEnabled环境变量:它将为您提供有关导致问题的对象的更多信息。更多信息here