0

下面的代码有多么错误:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
    for (id currentObject in array) {

        if ([currentObject isKindOfClass:[self class]]) {
            self = [currentObject retain];
        }
    }

    return self;
}

我正在尝试从 nib 文件中调出一个自定义 UITableViewCell,问题是 Leaks Instruments 说里面有泄漏:

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

static NSString *CellIdentifier = @"Cell";

    CPVideoGenTVCell *cell = (CPVideoGenTVCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[[CPVideoGenTVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.titleLbl.textColor = [UIColor colorWithRed:0.992 green:0.933 blue:0.0 alpha:1.0];

    }

    CPTempVideo *aVideo = [self.videos objectAtIndex:indexPath.row];

    cell.seasonLbl.text = [NSString stringWithFormat:@"Season %@", [self romanNumberFromArabic:aVideo.season]];
    cell.titleLbl.text = [NSString stringWithFormat:@"%2i - \"%@\"",aVideo.idVideo, aVideo.chapterName];
    cell.managedImg.url = aVideo.thumbnail;

    [[CPDataManager sharedDataManager] manageImage:cell.managedImg];

    return cell;
}
4

2 回答 2

0

认为问题出在保留上,但不明白你为什么要用 initWithStyle 做这些疯狂的事情。

if ([currentObject isKindOfClass:[self class]]) {
  self = currentObject;
}
于 2012-05-09T23:11:31.383 回答
0

我发现了另一种选择:

+ (id)cellWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
    for (id currentObject in array) {

        if ([currentObject isKindOfClass:[self class]]) {
            return currentObject;
        }
    }

    return nil;
}

只需返回一个自动释放的对象并在 cellForRowAtIndexPath 中处理它:

CPTempHeadCell *headCell = (CPTempHeadCell *)[tableView dequeueReusableCellWithIdentifier:HeadCellIdentifier];
if (headCell == nil) {

    headCell = [CPVideoGenTVCell cellWithStyle:UITableViewCellStyleDefault 
                                       reuseIdentifier:CellIdentifier];
于 2012-05-11T16:43:16.927 回答