UITableView附件视图未显示

时间:2011-06-02 16:16:07

标签: iphone objective-c uitableview imageview

为什么以下内容未显示正确的UITableView附件视图?它只是显示在nib文件中选择的UIAcessoryTypeCheckmark。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForManagedObject:(NSManagedObject *)managedObject withIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Selection";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) {
        cell = tvCell;
        self.tvCell = nil;
    }   
    self.tableView.rowHeight = 70.0f;

    //Background
    BOOL useDarkBackground = NO; //odd
    if(indexPath.row %2 == 0) useDarkBackground = YES; //even
    NSString *backgroundImagePath = [[NSBundle mainBundle] pathForResource:useDarkBackground ? @"DarkBackground" : @"LightBackground" ofType:@"png"];
    UIImage *backgroundImage = [[UIImage imageWithContentsOfFile:backgroundImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
    cell.backgroundView = [[[UIImageView alloc] initWithImage:backgroundImage] autorelease];
    cell.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    cell.backgroundView.frame = cell.bounds;

    //Checkmark
    cell.userInteractionEnabled = YES;
    NSString *checkmarkImagePath;
    checkmarkImagePath = [[NSBundle mainBundle] pathForResource:[selectedObjects containsObject:managedObject] ? @"checkSelected" : @"checkUnselected" ofType:@"png"];
    if([selectedObjects count] == 0) {
        checkmarkImagePath = [[NSBundle mainBundle] pathForResource:@"checkUnselected" ofType:@"png"];
    }   
            UIImage *checkmarkImage = [[UIImage imageWithContentsOfFile:checkmarkImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:checkmarkImage] autorelease];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.frame = CGRectMake(10.0, 10.0, imageView.frame.size.width/1.2, imageView.frame.size.height/1.2);

    return cell;

}

出于某种原因,以下 工作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *objectToChange = [[self fetchedResultsControllerForTableView:tableView] objectAtIndexPath:indexPath]; //The object

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

NSString *checkmarkImagePath = [[NSBundle mainBundle] pathForResource:[selectedObjects containsObject:objectToChange] ? @"checkSelected" : @"checkUnselected" ofType:@"png"];
UIImage *checkmarkImage = [[UIImage imageWithContentsOfFile:checkmarkImagePath] stretchableImageWithLeftCapWidth:0.0 topCapHeight:1.0];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:checkmarkImage] autorelease];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = CGRectMake(10.0, 10.0, imageView.frame.size.width/1.2, imageView.frame.size.height/1.2);

cell.accessoryView = imageView;
}

非常感谢!

2 个答案:

答案 0 :(得分:2)

看起来你没有将UIImageView添加到第一个代码块中单元格的附件视图中。

你也在整个地方泄漏。确保您遵守Apple提出的memory management guidelines

答案 1 :(得分:2)

在你的cellForRowAtIndexPath中,你没有像在didSelect方法中那样将accessoryView分配给单元格。