IOS 中的多个可变数组

Multiple Mutable Array in IOS

我是 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;
}

名称中的图像数组计数为 2.But,时间数组 1 对象仅为 there.So 您必须为三个数组提供相同的大小。

试试这个代码

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;
}
- (NSInteger)tableView:(UITableView *)tableView   numberOfRowsInSection:    (NSInteger)section
{return image.count;}

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

在您的代码中,图像计数为 2,因此更改 ,因为单元格将根据 table 视图中的节数创建 2 次,因为名称和时间数组每个仅包含 1 个对象,因此添加两个 NSMutableArray.

中的对象数量相等

您将获得预期的输出。或者您可以尝试使用键值对的 NSDictionary。