gpt4 book ai didi

objective-c - UIPickerView 自定义 View 不可视化

转载 作者:行者123 更新时间:2023-11-29 04:52:48 25 4
gpt4 key购买 nike

我有自定义 View :

#import <UIKit/UIKit.h>
@interface materialView : UIView {
IBOutlet UIImageView * _img;
IBOutlet UILabel * _title;
}
- (id)initWithTitle:(NSString*)pTitle image:(UIImage *)pImage;
@end

#import "materialView.h"
@implementation materialView
- (id)initWithTitle:(NSString*)pTitle image:(UIImage *)pImage {
CGRect frame = CGRectMake(0, 0, 295, 38);
self = [super initWithFrame:frame];
if (self) {
[_img setImage:pImage];
[_title setText:pTitle];
}
return self;
}
@end

然后我将此 View 添加到我的 UIPickerView 中:

- (id)init {
self=[super init];
if (self) {
materialView * pl = [[materialView alloc] initWithTitle:@"Plastic" image:[UIImage imageNamed:@"aaa.jpeg"]];
materialView * gl = [[materialView alloc] initWithTitle:@"Glass" image:[UIImage imageNamed:@"bbb.jpeg"]];
_loadedItems = [[NSArray arrayWithObjects:pl, gl, nil] retain];
}
return self;
}
...

pickerView.dataSource = self;
pickerView.delegate = self;
...

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
return [_loadedItems objectAtIndex:row];
}

问题是,我的行为很奇怪。我的 View 未显示在选取器 View 中。有谁可以帮助我吗?

最佳答案

首先,在您的:

- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view

测试 _loadedItews 和通过 objectAtIndex: 返回的对象不是 nil?如果没有更多上下文,您的代码看起来很合理。

还将自定义 View 的 userInteractionEnabled 属性设置为 NO。似乎如果将其设置为 YES,则自定义 View 会拦截触摸,并且选择器无法滚动到点击的行。

关于objective-c - UIPickerView 自定义 View 不可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8535159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com