如何重新加载当前的UIView

时间:2012-01-31 21:44:54

标签: iphone ios uitableview uiview

我从主视图调用的方法是设置要在滚动视图中显示的图像的图像名称。

 - (void)loadImage:(NSString *)myImageName
    {
        if (myImageName == @"one") {
            imageName = myImageName;
        }
        if (myImageName == @"two") {
            imageName = myImageName;
        }
        if (myImageName == @"three") {
            imageName = myImageName;
        }

        //Reloads view here???
    }

我正在我的viewdidload方法中加载图像,如此

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Create scrollview
    scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    scrollView.delegate = self;
    scrollView.bounces = NO;

    //Create scrollviewimage
    if (imageName == @"one") {
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ha.png"]];
    }
    if (imageName == @"two") {
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"haha.png"]];
    }
    if (imageName == @"three") {
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hahaha.png"]];
    }



    containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 550)];
    //Add subview
    [containerView addSubview:image];
    //initViews
    scrollView.contentSize = containerView.frame.size;
    [scrollView addSubview:containerView];

    //scrolling
    scrollView.minimumZoomScale = 1.0;
    scrollView.maximumZoomScale = 31.0;
    [scrollView setZoomScale:scrollView.minimumZoomScale];

    //highrachy
    [self.view addSubview:scrollView];

}

当我从tableviewcell选择中设置父视图中的imagename时会发生什么我将nsstring传递给loadImage ..然后加载图像在viewdidload中设置名称..但是发生的事情是只有第一次选择剂量任何东西..因此,您将始终看到您正在选择的第一个图像..所以,如果您选择图像两,您选择的每个其他图像将显示图像二..

任何帮助都会很棒。

这是父视图单元格选择的样子

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];

    if (!self.detailViewController) {
        self.detailViewController = [[ICDDetailViewController alloc] initWithNibName:@"ICDDetailViewController" bundle:nil];

        if (indexPath.section == 0) {
            _detailViewController.imageName = @"one";
            //        NSLog(@"%@", indexPath);
        }
        if (indexPath.section == 1) {
            _detailViewController.imageName = @"two";
            //        NSLog(@"%@", indexPath);
        }
        if (indexPath.section == 2) {
            _detailViewController.imageName = @"three";
            //        NSLog(@"%@", indexPath);
        }
    }
    [self.navigationController pushViewController:self.detailViewController animated:YES];

}

3 个答案:

答案 0 :(得分:3)

不要使用myImageName == @“three”。有一种比较字符串的特殊方法。

试试这个:

if ([myImageName isEqualToString:@"three"]) {
    [image setImage:[UIImage imageNamed:myImageName]];
}

如果有文件扩展名,请执行以下操作:

NSString *imagePath = [NSString stringWithFormat:@"%@.png",myImageName];
[image setImage:[UIImage imageNamed:imagePath]];

顺便说一句,你还在发布你在其他问题中的旧代码。您将在原始类中保留detailView,而不是释放和创建新实例。取出if(!detailView)语句。

更新:唯一的另一个选择是从大的if()块中取出赋值。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];

    if (!self.detailViewController) {
        self.detailViewController = [[ICDDetailViewController alloc] initWithNibName:@"ICDDetailViewController" bundle:nil];


    //MY CHANGES HERE:
    //If you left these if () statements inside the original code, they would never
    //fire if the detailView was already instantiated once.  Does this make sense?
    }  // <--- moved the bracket up here
        if (indexPath.section == 0) {
            _detailViewController.imageName = @"one";
            //        NSLog(@"%@", indexPath);
        }
        if (indexPath.section == 1) {
            _detailViewController.imageName = @"two";
            //        NSLog(@"%@", indexPath);
        }
        if (indexPath.section == 2) {
            _detailViewController.imageName = @"three";
            //        NSLog(@"%@", indexPath);
        }
    //}  <--- commented this one out
    [self.navigationController pushViewController:self.detailViewController animated:YES];

}

唯一的另一种方式是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Sets the back button for the new view that loads (this overrides the usual parentview name with "Back")
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];
    //commented out the next line, don't need to check to see if it's already present
    //just create a new instance to push on the stack
    //if (!self.detailViewController) {
        self.detailViewController = [[ICDDetailViewController alloc] initWithNibName:@"ICDDetailViewController" bundle:nil];

        if (indexPath.section == 0) {
            _detailViewController.imageName = @"one";
            //        NSLog(@"%@", indexPath);
        }
        if (indexPath.section == 1) {
            _detailViewController.imageName = @"two";
            //        NSLog(@"%@", indexPath);
        }
        if (indexPath.section == 2) {
            _detailViewController.imageName = @"three";
            //        NSLog(@"%@", indexPath);
        }
    //} <--- get rid of this bracket since the original if () was commented out
    [self.navigationController pushViewController:self.detailViewController animated:YES];

}

答案 1 :(得分:1)

听起来你并没有发布你的UIScrollView实例。因此,当您第二次重复使用它时,不会再次调用viewDidLoad。这意味着您的“图像”变量永远不会重新分配新图像。

要检查是否是这种情况,请尝试在viewDidLoad方法中放置一个NSLog调用,看看是否在第二次从tableViewCell中选择时调用它。

如果是这种情况,可以通过几种方法进行修复。你在使用ARC吗?

答案 2 :(得分:0)

您是否尝试过更新UIImageView

- (void)loadImage:(NSString *)myImageName
{
    if (myImageName == @"one") {
        imageName = myImageName;
    }
    if (myImageName == @"two") {
        imageName = myImageName;
    }
    if (myImageName == @"three") {
        imageName = myImageName;
    }

    [image setImage:[UIImage imageNamed:imageName];
}