【问题标题】:Adding images stored in NSArray to UITableView将存储在 NSArray 中的图像添加到 UITableView
【发布时间】:2014-08-01 13:49:36
【问题描述】:

首先,我是编程新手,我正在尝试制作一个具有UITableView 并显示存储在数组中的名称并显示除每个名称之外的图像(图标)的应用程序。我在包中添加了三个图像,并将它们命名为 1.jpg2.jpg3.jpg

应用程序运行良好,但在告诉应用程序我要显示存储在数组中的图像并运行应用程序后,iOS 模拟器首先可以工作,但随后我的 main.m 文件中出现绿色错误?

下面是我的实现文件:

NSMutableArray *myImages;
NSMutableArray *myNames;

@implementation ViewController

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

    myNames = [[NSMutableArray alloc ]initWithObjects:@"A",@"B",@"C", nil];

    for (int imageNumber =1 ; imageNumber <= 3; imageNumber++) {
            myImages= [[NSMutableArray alloc]initWithObjects:[[NSString stringWithFormat:@"%i.jpg", imageNumber ]], nil];
      }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return myNames.count;
 }


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];

     cell.textLabel.text = myNames[indexPath.row];

    //everything works fine until i add this line

    cell.imageView.image = myImages [indexPath.row];
    return cell;
}

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

您的数组中填充了文件名,而不是图像。您需要使用文件名获取图像。您可以使用UIImageimageNamed 方法来做到这一点。这将从应用程序包中获取您的图像。

试试这个:cell.imageView.image = [UIImage imageNamed:myImages [indexPath.row]];

此外,在您的 viewDidLoad: 中,每次循环循环时您都在重新创建 NSMutableArray。

尝试将您的 viewDidLoad 替换为:

- (void)viewDidLoad
{
    [super viewDidLoad];
    myNames = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C", nil];

    for (int imageNumber =1 ; imageNumber <= 3; imageNumber++) 
    {
        [myImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
    }

}

【讨论】:

    【解决方案2】:

    试试dis。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
    
        myNames = [[NSMutableArray alloc ]initWithObjects:@"A",@"B",@"C", nil];
        myImages= [[NSMutableArray alloc]initWithObjects:[UIImage imagNamed @"1.jpg"],[UIImage imagNamed @"2.jpg"],[UIImage imagNamed @"3.jpg"],];
    
    }
    
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        return myNames.count;
    }
    
     -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1; //number of sections in the table. 1 meaning only 1 section.
    }
    
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.myImageView.image = [myImages objectAtIndex:indexPath.row];
         cell.myTextLabel.text = myNames[indexPath.row];
    
        return cell;
    }
    
    
    ;
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    

    【讨论】:

      【解决方案3】:
      [myImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
      

      【讨论】:

      • 谢谢.. 看来我必须在循环之前声明一个空数组myImages = [NSMutableArray array]
      【解决方案4】:

      您的图像名称数组应与名称数组的长度相匹配。将它们都配置为文字并尝试。也就是说,像这样初始化数组:

      NSArray *myNames = @[@"A", @"B", @"C"];
      NSArray *myImages = @[@"1.jpg", @"2.jpg", @"3.jpg"];
      

      您还可以通过更改图像循环来修复它,这样它就不会在每次迭代时创建新的 NSMutableArray:

      myImages = [NSMutableArray array];
      for (int imageNumber =1 ; imageNumber <= 3; imageNumber++) {
          [myImages addObject:[NSString stringWithFormat:@"%i.jpg", imageNumber]];
      }
      

      请注意,您需要使用图像名称创建图像:

      UIImage *actualImage = [UIImage imageNamed:myImages[indexPath.row]];
      

      【讨论】:

      • 完美答案。但我想知道名称数组长度是否必须与图像数组的长度相匹配,否则应用程序将无法工作?
      • 唯一真正的要求是 myImages 数组至少与您在 numberOfRowsInSection 中使用的任何内容一样长(这样如果表格视图要求第 10 行,您最终不会尝试访问 3 元素数组的第 10 个元素)。您还可以创建一个具有属性(例如 imageName、title)的新类来保存您感兴趣的信息并使用这些对象填充单个数组。
      【解决方案5】:

      好的,Objective-C 在处理数组的方式上与其他编程语言不同。在objective-c 中,数组可以保存any 对象指针。所以在你的行中

      myImages= [[NSMutableArray alloc]initWithObjects:[[NSString stringWithFormat:@"%i.jpg", imageNumber ]], nil];
      

      您实际上并没有创建图像并将它们添加到数组中,而是一遍又一遍地创建数组。改为这样做

      [myImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]]];
      

      【讨论】:

      • 谢谢.. 看来我必须在循环之前声明一个空数组myImages = [NSMutableArray array];
      【解决方案6】:

      几个cmets:

          myNames = [[NSMutableArray alloc ]initWithObjects:@"A",@"B",@"C", nil];
      

      最好写成

          NSArray *myNames = @[@"A",@"B",@"C"];
      

      现在,你的问题线

          cell.imageView.image = myImages [indexPath.row];
      

      是类型不匹配。 cell.imageView.image 是 UImage* 类型。您不能用字符串分配它。 也许这会起作用:

      cell.imageView.image = [UIImage imageNamed:myImages [indexPath.row]];
      

      HTH

      -伊泰

      【讨论】:

        【解决方案7】:

        viewDidLoad 中的问题是你在循环中分配你的数组

        - (void)viewDidLoad
        {
            [super viewDidLoad];
        
            myNames = [[NSMutableArray alloc ]initWithObjects:@"A",@"B",@"C", nil];
            myImages= [[NSMutableArray alloc]init]; 
            for (int imageNumber =1 ; imageNumber <= 3; imageNumber++) {
                [myImages addObject:[NSString stringWithFormat:@"%i.jpg", imageNumber ]];
            }
        }
        

        cellForrowAtIndexPath 中的问题是您设置 NSString 代替 UIImage

        -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
            cell.textLabel.text = myNames[indexPath.row];
        
            cell.imageView.image = [UIImage imageNamed:myNames[indexPath.row];
            return cell;
        }
        

        把这两个方法换掉就行了

        【讨论】:

          猜你喜欢
          • 2011-06-27
          • 1970-01-01
          • 2017-05-12
          • 1970-01-01
          • 1970-01-01
          • 2011-08-17
          • 1970-01-01
          • 1970-01-01
          • 2020-07-21
          相关资源
          最近更新 更多