1

我有一个非常简单但令人沮丧的问题。在我的didSelectRowAtIndexPath:方法中,我试图更改UIImageView另一个视图,但不幸的是,它不起作用,并且UIImageView保持不变。

我究竟做错了什么?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MainViewController *demoController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    demoController.title = [NSString stringWithFormat:@"Demo Controller #%d-%d", indexPath.section, indexPath.row];

    NSArray *controllers = [NSArray arrayWithObject:demoController];
    self.sideMenu.navigationController.viewControllers = controllers;
    [self.sideMenu setMenuState:MFSideMenuStateClosed];

    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"server"] isEqualToString:@"imap.mail.me.com"]){
        if (indexPath.row == 0)
            theAppDelegate.folder = @"INBOX";
            UIImage *image = [UIImage imageNamed:@"inbox.png"];
            demoController.imageBG.image = image;

        if (indexPath.row == 1)
            theAppDelegate.folder = @"Sent Messages";
        UIImage *image2 = [UIImage imageNamed:@"sentmail.png"];
        demoController.imageBG.image = image2;

        if (indexPath.row == 2)
            theAppDelegate.folder = @"Drafts";
        UIImage *image3 = [UIImage imageNamed:@"drafts.png"];
        demoController.imageBG.image = image3;

        if (indexPath.row == 3)
            theAppDelegate.folder = @"Deleted Messages";
        UIImage *image4 = [UIImage imageNamed:@"trash.png"];
        demoController.imageBG.image = image4;

        if (indexPath.row == 4)
            theAppDelegate.folder = @"Archive";
        UIImage *image5 = [UIImage imageNamed:@"archive.png"];
        demoController.imageBG.image = image5;

        if (indexPath.row == 5)
            theAppDelegate.folder = @"Junk";
        UIImage *image6 = [UIImage imageNamed:@"junk.png"];
        demoController.imageBG.image = image6;

    }
}
4

2 回答 2

0

照此行if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"server"] isEqualToString:@"imap.mail.me.com"])必得真意,你demoController.imageBG.image总有junk.png

我想你在身体{}之间失踪了。if

它必须是这样的

if (indexPath.row == 0)
{
    theAppDelegate.folder = @"INBOX";
    UIImage *image = [UIImage imageNamed:@"inbox.png"];
    demoController.imageBG.image = image;    
}
 if (indexPath.row == 1)
     .........

注意:在编写此类编码时,最好使用else-ifOR switch-case

于 2013-04-17T03:43:04.983 回答
0

拳头你错过{}if条件,所以在这里你每次都能得到junk.png图像......UIImageView

也在这里,而不是你的这个逻辑,你可以简单地传递图像的名称NSString......

只需对字符串进行属性合成并MainViewController像下面这样传入您的类...

=>MainViewController.h

@property (nonatomic, retain)   NSString *strImageName;

=>MainViewController.m

@synthesize strImageName;

并像上面的代码一样在这个变量中传递这个名称......

   if (indexPath.row == 0){
    theAppDelegate.folder = @"INBOX";
    demoController.strImageName = @"inbox.png";
    [demoController.strImageName retain];
   }
   else if (indexPath.row == 1){
        theAppDelegate.folder = @"Sent Messages";
    demoController.strImageName = @"sentmail.png";
    [demoController.strImageName retain];
   }

并在您MainViewController的 viewDidLoad 方法中分配此图像,如下所示。

 - (void)viewDidLoad
{
        [super viewDidLoad];
        UIImage *image = [UIImage imageNamed:strImageName];
        imageBG.image = image;
}
于 2013-04-17T03:55:27.297 回答