ios - 如何在UIpickerView的特定行中插入图像和文本?

标签 ios cocoa-touch uikit uipickerview

我已经实现了以下功能:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UIImage *img = [UIImage imageNamed:@"persona.jpeg"];
    UIImageView *temp = [[UIImageView alloc] initWithImage:img]; 
    temp.frame = CGRectMake(-70, 10, 60, 40);

    UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -5, 80, 60)];
    channelLabel.text = @"persona y";
    channelLabel.textAlignment = UITextAlignmentLeft;
    channelLabel.backgroundColor = [UIColor clearColor];

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
    [tmpView insertSubview:temp atIndex:0];
    [tmpView insertSubview:channelLabel atIndex:1];

    return tmpView; 
}

结果是:

我需要在特定的行中插入图像和文本,知道怎么做吗?
非常感谢您的帮助。

我的想法是填充图片阵列和描述以填充UIPickerView

最佳答案

尝试获取数据作为ArrayNSDictionary

字典结构:{Image:<imageName>, LabelText:<labelText>}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
{
    NSDictionary *dict =  [datasourceArray objectAtIndex:row];
    UIImage *img = [UIImage imageNamed:[dict valueForKey:@"Image"]]; // get image path from the dictionary
    UIImageView *temp = [[UIImageView alloc] initWithImage:img]; 
    temp.frame = CGRectMake(-70, 10, 60, 40);

    UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -5, 80, 60)];
    channelLabel.text = [dict valueForKey:@"LabelText"];
    channelLabel.textAlignment = UITextAlignmentLeft;
    channelLabel.backgroundColor = [UIColor clearColor];

    UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
    [tmpView insertSubview:temp atIndex:0];
    [tmpView insertSubview:channelLabel atIndex:1];

    return tmpView;
}

关于ios - 如何在UIpickerView的特定行中插入图像和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12251315/

相关文章:

ios - 从 Yandex API 更改标注的大小

iOS - 内容 View 非常大的 UIScrollView,如何减少内存使用?

iphone - UITableViewCell 的高度如何设置

iphone - iOS : Detecting when the multitasking bar has been opened

android - 如何更改应用程序的内容

ios - 使用 Swift 包管理器添加依赖项

ios - 链接 UIScrollView 和 UIPageControl

iphone - 加快 iPhone 3G 上的全屏图像绘制

iphone - 对于 "as-you-type"搜索,Core Data 是否比自定义索引更有效?

ios - NSOperationQueue 导致 Crashlytics 崩溃(QOS : UNSPECIFIED)