简体   繁体   中英

How to set UITableViewCell identifier in xcode?

I am getting a crash randomly when I select a cell in a UITableView in a viewcontroller in my project.

I think the problem is caused by the cells I am trying to reuse.

Here is the code.

- (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];
}

I create my table in XCode. So there is no UITableViewCell object in IB which has an identifier.

Is it possible to add this in code while creating the table? Or is the problem somewhere else?

Thanks

When you get a EXC_BAD_ACCESS, usually means that there is an incorrect memory management (ie an object was improperly released). My suggestion is to enable the NSZombieEnabled environment variable: it will give you more information about what is the object that causes the problem. More info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM