0

男士们,我正在尝试将图像设置为表格视图的页脚。页脚图像需要根据点击导航栏中的按钮进行更改。(设计是这样的导航栏上有两个按钮,当加载屏幕/视图时,页脚中有一个特定的图像,点击其中一个按钮会改变它,然后点击另一个按钮会改变它并且很快)。

我尝试使用 UIImage aa @property,然后尝试更改图像并在其上设置框架,但这不起作用(框架设置不正确)。本质上,我想做这样的事情:

[[self table1] setfooterview : [[uiimageview alloc] initwithframe:(CGRectMake (0,0,50,50)] and then I also need to add initwithimage:[UIImage imagenamed :@"hello-1.png"]] 

但问题是我不能在同一个语句中执行两个初始化方法。而且我无法创建像 UIImageView *hello 之类的对象,因为稍后我需要在不同的方法中使用它(在我处理不同按钮按下的方法中)。(稍后,我使用一个简单的 if 语句,然后根据按下的按钮触发页脚视图上的图像)。我的猜测是我可能必须做类似
[[self table1] setfooterview: [[uiimageview alloc] initwithframe :(CGRectMake (0,0,50,50)] 然后做一个设置图像,但我认为因为 setimage 不是有效,我可能需要以某种方式投射它。我该怎么做?

抱歉语法很糟糕,请忽略所有语法错误,因为我的 mac 目前不在我身边,我只是从内存中输入这个,而且我是 iOS 编程的新手。

4

3 回答 3

1

您不需要发送两个 init 方法。图像和框架都是 的属性UIImageView。(该frame属性继承自UIView。)您可以在发送初始化消息后设置它们。

你可以这样做:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(...)];
imageView.image = [UIImage imageNamed:@"hello-1"];
self.table1.footerView = imageView;

或者你可以这样做:

UIImageView *imageView = [[UIImageView alloc] initWithImage:
    [UIImage imageNamed:@"hello-1"];
imageView.frame = CGRectMake(...);
self.table1.footerView = imageView;

或者你甚至可以这样做:

UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(...);
imageView.image = [UIImage imageNamed:@"hello-1"];
self.table1.footerView = imageView;
于 2013-02-11T07:51:50.463 回答
0

没有理由调用两个 init 方法。您可以继承或扩展 UIImageView 并实现一个新的 init 方法,该方法允许一次性设置框架和图像。但是您也可以执行以下操作。

UIImageView footerView =  [[uiimageview alloc] initwithframe:(CGRectMake (0,0,50,50)];
footerView.image =  [UIImage imagenamed :@"hello-1.png"];
[[self table1] setfooterview: footerView];

我刚刚键入的替代方案。这不是 QAed,可能不是完美的解决方案,但应该给你一个正确方向的提示。在您的 .h 文件中添加以下两行之一:

- (void) changeFooterImage:(UIImage *) image;
- (void) changeFooterImageNamed:(NSString *)imageName;

在您的 .m 文件中添加相应的实现:

- (void) changeFooterImage:(UIImage *) image {
    if (!self->footerView) {
        self->footerView = [[UIImageView alloc] initWithFrame:CGRectMake (0,0,50,50)];
        [[self tableView] setTableFooterView: footerView];
    }
    self->footerView.image =  image;
}

- (void) changeFooterImageNamed:(NSString *)imageName{
    if (!self->footerView) {
        self->footerView = [[UIImageView alloc] initWithFrame:CGRectMake (0,0,50,50)];
        [[self tableView] setTableFooterView: footerView];
    }
    self->footerView.image =  [UIImage imageNamed :imageName];
}

然后,您的第 3 方视图控制器应包含此视图控制器的 .h 文件。更“整洁”和“出书”的方式是不要以这种方式直接绑定类。您应该更喜欢在协议中声明此方法,然后实现该协议,并且在实际调用该方法之前,您可以检查该类是否实现了该协议。但我觉得所有这些都有点太多新东西了。从这个开始,然后在原则上可行时实现协议。

于 2013-02-11T07:54:53.323 回答
0

“我不能创建像 UIImageView *hello 这样的对象,因为我以后需要在不同的方法中使用它”这是什么意思?

您可以通过任何方式初始化 UIImageView 并在将其分配给 footerView 之前设置其框架和初始图像。

于 2013-02-11T08:03:23.583 回答