【问题标题】:How insert an images and text in specific rows of an UIpickerView?如何在 UIpickerView 的特定行中插入图像和文本?
【发布时间】:2012-09-04 23:46:47
【问题描述】:

我已经实现了以下功能:

- (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

【问题讨论】:

  • 你是说它像一个 UITableView 吗?我不太明白你的问题。
  • 图像和文本被复制到我需要在特定行中插入图像和文本的所有行中,因为然后我用不同的图像加载选择器视图,然后是图像的描述

标签: ios cocoa-touch uikit uipickerview


【解决方案1】:

尝试以Array of NSDictionary 的形式获取您的数据。

字典结构:{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;
}

【讨论】:

  • iShrey 是否应该使用相同的逻辑来实现具有文本、图像和位置数据类型的聊天应用程序?考虑在“字典消息数组”中检测消息的数据类型,例如 {Image:, LabelText:, Location:} 根据每种数据类型配置我的 tableView 单元格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多