【问题标题】:Multiple Mutable Array in IOSIOS中的多个可变数组
【发布时间】:2016-02-23 07:20:03
【问题描述】:

我对 ios 比较陌生,谁能告诉我如何管理多个可变数组?例如:我有 3 个数组,其中一个数组用于图像,另外两个数组包含名称和描述,我想在 UITableView 单元格上显示

我试过这个,但它崩溃了。

image = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"demo_business_image@.png"], [UIImage imageNamed:@"demo_business_image@.png"], nil];
name = [[NSMutableArray alloc] initWithObjects:@"Dinner Bell Annouse 50% Discount on Punjabi Food!", nil];
time = [[NSMutableArray alloc] initWithObjects:@"13M", nil];

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIImageView *img = (UIImageView *) [cell.contentView viewWithTag:720];
    UILabel *lbl1 = (UILabel *) [cell.contentView viewWithTag:721];
    UILabel *lbl2 = (UILabel *) [cell.contentView viewWithTag:722];
    img.image = [image objectAtIndex:indexPath.row];
//    lbl1.text=[name objectAtIndex:indexPath.row];
//    lbl2.text=[time objectAtIndex:indexPath.row];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    return cell;
}

【问题讨论】:

  • 什么是崩溃错误?
  • 你能把你的崩溃日志贴在这里吗?

标签: ios objective-c iphone nsmutablearray


【解决方案1】:

试试这个代码

image=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"demo_business_image@.png"], nil];
name=[[NSMutableArray alloc]initWithObjects:@"Dinner Bell Annouse 50% Discount on Punjabi Food!", nil];
time=[[NSMutableArray alloc]initWithObjects:@"13M", nil];

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


- (UITableViewCell *)tableView:(UITableView *)tableView   cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *CellIdentifier = @"Cell";
 UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 UIImageView *img=(UIImageView*)[cell.contentView viewWithTag:720];
 UILabel *lbl1=(UILabel *)[cell.contentView viewWithTag:721];
 UILabel *lbl2=(UILabel *)[cell.contentView viewWithTag:722];
 img.image=[image objectAtIndex:indexPath.row];
 //    lbl1.text=[name objectAtIndex:indexPath.row];
//    lbl2.text=[time objectAtIndex:indexPath.row];

if (cell == nil)
{
   cell = [[UITableViewCell alloc]  initWithStyle:UITableViewCellStyleSubtitle   reuseIdentifier:CellIdentifier];
}
   return cell;
}

【讨论】:

    【解决方案2】:

    图像数组计数为 2。但在名称和时间数组中只有 1 个对象存在。所以你必须为三个数组提供相同的大小。

    【讨论】:

    • 是的,我已经这样做了,但我已经拿了一个 NSMutableDictionary 并将所有数组存储到其中。但是它崩溃 data=[[NSMutableDictionary alloc]init]; [data setObject:name forKey:@"name"]; [data setObject:image forKey:@"image"]; [数据集对象:dis forKey:@"discrip"];
    • 像这样创建字典 NSDictionary *DIc=[NSDictionary DictionarywithObjectsAndkeys:[UIImage imageNamed:@"demo_business_image@.png"],@"Image",@"Dinner Bell Annouse 旁遮普食品 50% 折扣!","姓名",@"13M",@"时间",nil];并将 Dic 对象存储在 nsmutable 数组 NSMutableArray *Arr=[NSMutableArray alloc]init]; [Arr addObject:Dic];
    【解决方案3】:
    - (NSInteger)tableView:(UITableView *)tableView   numberOfRowsInSection:    (NSInteger)section
    {return image.count;}
    

    image.count 结果是您的编码中的 2 个值(来自图像数组的值), 当 cellForRowAtIndexPath 为每个数组调用 2 次时 但在其他数组中只有1个值,它必须显示绑定数组的错误索引

    【讨论】:

      【解决方案4】:

      在您的代码中,图像计数为 2,因此请更改,因为单元格将根据表格视图中的部分数创建 2 次,因为名称和时间数组每个仅包含 1 个对象,因此在其中添加相等数量的对象都是 NSMutableArray。

      然后你会得到预期的输出。或者您可以尝试使用键值对的 NSDictionary。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-27
        相关资源
        最近更新 更多