如何更改在ipad应用程序中选择的单元格的图像

时间:2013-07-03 05:23:55

标签: objective-c uitableview

我正在使用带有自定义单元格的tableView我希望当用户选择任何行时,它显示所选图像,这里是代码,但它不显示所选图像

以下是我为此tableView使用自定义单元格的代码

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

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
    cell = (RootCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RootCell"
                                                     owner:self options:nil];
        for(id oneObject in nib) {
            if ([oneObject isKindOfClass:[RootCell class]]) {
                cell = (RootCell *)oneObject;
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
            }
        }
    }
    if (indexPath.row == 0) {
        cell.cellImageView.image=[UIImage imageNamed:@"ActivityF.png"];  
    } else if(indexPath.row==1) {
        cell.cellImageView.image=[UIImage imageNamed:@"CatalogF.png"];
    } else if(indexPath.row==2){
        cell.cellImageView.image=[UIImage imageNamed:@"LibraryF.png"];  
    } else if(indexPath.row==3) {
        cell.cellImageView.image=[UIImage imageNamed:@"CommunityF.png"]; 
    } else if (indexPath.row==4) {
        cell.cellImageView.image=[UIImage imageNamed:@"ReportsF.png"];
    } else if (indexPath.row==5) {
        cell.cellImageView.image=[UIImage imageNamed:@"PublishingF.png"];
    } else if (indexPath.row==6) {
        cell.cellImageView.image=[UIImage imageNamed:@"PostsF.png"];    
    } else {
        cell.cellImageView=[UIImage imageNamed:@"7.png"];  
    }

    UIImageView *separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separatorline.png"]];
    [cell.contentView addSubview: separator];

    return cell;

    self.tableView.backgroundColor = [UIColor colorWithRed:(149/255.f) green:(150/255.f) blue:(149/255.f) alpha:1.0f];
}

选择了方法

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSUInteger row = indexPath.row;
    [self.appDelegate.splitViewController viewWillDisappear:YES];
    NSMutableArray *viewControllerArray=[[NSMutableArray alloc] initWithArray:    [[self.appDelegate.splitViewController.viewControllers objectAtIndex:1] viewControllers]];
    [viewControllerArray removeLastObject];
    if (row == 0) {
        self.firstDetailViewController=[[[FirstDetailViewController alloc] init]autorelease];
        [viewControllerArray addObject:self.firstDetailViewController];
        self.appDelegate.splitViewController.delegate = self.firstDetailViewController; }

    if (row == 1) {
        self.secondDetailViewController=[[[SecondDetailViewController alloc]init]autorelease];
        [viewControllerArray addObject:self.secondDetailViewController];
        self.appDelegate.splitViewController.delegate = self.secondDetailViewController;
    }

    if (row == 2) {
        self.myLearningViewController=[[[MyLearningViewController alloc]init]autorelease];
        [viewControllerArray addObject:self.myLearningViewController];
        self.appDelegate.splitViewController.delegate = self.myLearningViewController;
    }
}

2 个答案:

答案 0 :(得分:0)

在didSelectRowAtIndexPath中使用:

RootCell *cell = (RootCell *)[self.tableView cellForRowAtIndexPath: indexPath];
cell.cellImageView.image=[UIImage imageNamed:@"selectedImage.png"];

答案 1 :(得分:0)

在你的didSelectRowAtIndexPath:方法

 RootCell *cell = (RootCell *)[self.tableView cellForRowAtIndexPath: indexPath];


    if (indexPath.row == 0) {

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell1.png"];

}
else if(indexPath.row==1){

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell2.png"];

}

else if(indexPath.row==2){

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell3.png"];

}

else if(indexPath.row==3){

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell4.png"];

}

else if(indexPath.row==4){

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell5.png"];

}

else if(indexPath.row==5) {

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell6.png"];

}

else if (indexPath.row==6){

    cell.cellImageView.image=[UIImage imageNamed:@"selectedImagecell7.png"];

    }

else {
    cell.cellImageView=[UIImage imageNamed:@"other.png"];

 }