Ios 在tableview中显示图像';s下一个视图控制器上的单元格

Ios 在tableview中显示图像';s下一个视图控制器上的单元格,ios,uitableview,Ios,Uitableview,详细视图 这是显示错误部分的detailview代码。如何显示汽车不同部分的详细信息,如suv、轿车、掀背车。此代码以图形形式显示suv、轿车、掀背车类型的不同汽车 - (void)viewDidLoad { Hatchback = [[NSMutableArray alloc]initWithObjects:@"Hyundai i10",@"Hyundai i20",@"Maruti Suzuki Swift",@"Maruti Suzuki wagonR",@"Honda brio"

详细视图 这是显示错误部分的detailview代码。如何显示汽车不同部分的详细信息,如suv、轿车、掀背车。此代码以图形形式显示suv、轿车、掀背车类型的不同汽车

- (void)viewDidLoad
{
    Hatchback = [[NSMutableArray alloc]initWithObjects:@"Hyundai i10",@"Hyundai i20",@"Maruti Suzuki Swift",@"Maruti Suzuki wagonR",@"Honda brio",@"Ford figo", nil];


    SUV = [[NSMutableArray alloc]initWithObjects:@"Tata safari storme",@"Mahindra scorpio",@"Mahindra xuv 500",@"Renault duster", nil];


    Sedan = [[NSMutableArray alloc]initWithObjects:@"Honda city",@"Maruti Suzuki dzire",@"Hyundai verna",@"Skoda octavia",@"Honda civic",@"Honda amaze",@" Ford fiesta", nil];



    HatchbackImg = [[NSMutableArray alloc]initWithObjects:@"i10.jpg",@"i20.jpg",@"Swift.jpeg",@"wagonR.jpg",@"brio.jpg",@"figo.jpg",@"indica.jpg",@"beat.jpg",nil];


    SUVImg = [[NSMutableArray alloc]initWithObjects:@"safari.jpg",@"scorpio.jpg",@"xuv.jpg",@"duster.jpg", nil];


    SedanImg = [[NSMutableArray alloc]initWithObjects:@"city.jpg",@"dzire.jpg",@"verna.jpg",@"octavia.jpg",@"civic.jpg",@"amaze.jpg",@"fiesta.jpg", nil];


    [super viewDidLoad];

    self.tableView.backgroundColor = [UIColor brownColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    #pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    #warning Incomplete method implementation.
    // Return the number of rows in the section.

    //return 0;
    if (Carint ==0) {
        return [Hatchback count];
    } else if (Carint ==1) {
        return [SUV count];
    } else if (Carint ==2) {
        return [Sedan count];
    }
    [self.tableView reloadData];

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
- (UIImage *)imageForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
    } else if (Carint == 1) {
        return [UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
    } else if (Carint == 2) {
        return [UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
    }

    return nil;
}

- (NSString *)titleForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [Hatchback objectAtIndex:indexPath.row];
    } else if (Carint == 1) {
        return [SUV objectAtIndex:indexPath.row];
    } else if (Carint == 2) {
        return [Sedan objectAtIndex:indexPath.row];
    }

    return nil;
}

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];

        cell.textLabel.text = [self titleForRow:indexPath.row];
        cell.imageView.image = [self imageForRow:indexPath.row];

        if (Carint == 0) {
            cell.imageView.image=[UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
            cell.textLabel.text =[Hatchback objectAtIndex:indexPath.row];
            UIImageView *imageView = [[UIImageView alloc]init];
            [cell.contentView addSubview:imageView];
        } else if (Carint == 1) {
            cell.imageView.image=[UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
            cell.textLabel.text = [SUV objectAtIndex:indexPath.row];
            UIImageView *imageView = [[UIImageView alloc]init];
            [cell.contentView addSubview:imageView];
        } else if (Carint == 2) {
            cell.imageView.image=[UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
            cell.textLabel.text = [Sedan objectAtIndex:indexPath.row];
            UIImageView *imageView = [[UIImageView alloc]init];
            [cell.contentView addSubview:imageView];
        }
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }

     // Configure the cell...
    return cell;
}

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cellBg1.png"]];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}


    #pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (Carint == 0)
    {
       NSString *imageName = [HatchbackImg objectAtIndex:indexPath.row];
        //[UIImageView setImage:[SedanImg objectAtIndex:indexPath.row]];

     //  [UIImageView setImage:[SedanImg objectAtIndex:indexPath.row]];

    }
    else if (Carint ==1)
    {
        NSString *imageName = [SUVImg objectAtIndex:indexPath.row];

    }
    else if (Carint ==2)
    {
        NSString *imageName = [SedanImg objectAtIndex:indexPath.row];

    }

    NSString *carName = [self titleForRow:indexPath.row];
    UIImage *carImage = [self imageForRow:indexPath.row];

    // Navigation logic may go here. Create and push another view controller.

    carSelectViewController *carDetailViewController = [[carSelectViewController alloc] initWithNibName:@"carSelectViewController" bundle:nil];

    carDetailViewController.carName = carName;
    carDetailViewController.carImage = carImage;

    [self.navigationController pushViewController:carDetailViewController animated:YES];

}

@end
然后按下或显示此视图控制器以显示

viewcontroller.image是下一个类中的cusotm属性 然后是viewdidload方法


将imageView的image属性设置为self.image

您不应该将数据存储在表的单元格中。表格的单元格用于显示信息

首先,您似乎在使用iVar
Carint
来显示不同的汽车。首先,所有变量都应该有小写的首字母。第二,这是个坏主意

通过这样做,您可以将表与其显示的数据强耦合。不过,我暂时不考虑这个问题

其次,您还应该使用自定义UITableViewCell子类来创建
UIImageViews
等。。。但我也会忽略这一点

第三,您应该创建一个名为
Car
的类。然后给这个类
Car
两个属性,
name
image
,实际上你也可以给它
type
,但我还是会忽略这一点

注意仅仅因为我忽略了它们并不意味着你也应该这样做。将前三项视为下一步工作的提示

第四,创建一个函数,返回该行的图像。像这样

- (UIImage *)imageForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
    } else if (Carint == 1) {
        return [UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
    } else if (Carint == 2) {
        return [UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
    }

    return nil;
}
- (NSString *)titleForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [Hatchback objectAtIndex:indexPath.row];
    } else if (Carint == 1) {
        return [SUV objectAtIndex:indexPath.row];
    } else if (Carint == 2) {
        return [Sedan objectAtIndex:indexPath.row];
    }

    return nil;
}
@interface carSelectViewController : UIViewController

    @property NSString *carName;
    @property UIImage *carImage;

@end
还有标题。像这样

- (UIImage *)imageForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
    } else if (Carint == 1) {
        return [UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
    } else if (Carint == 2) {
        return [UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
    }

    return nil;
}
- (NSString *)titleForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [Hatchback objectAtIndex:indexPath.row];
    } else if (Carint == 1) {
        return [SUV objectAtIndex:indexPath.row];
    } else if (Carint == 2) {
        return [Sedan objectAtIndex:indexPath.row];
    }

    return nil;
}
@interface carSelectViewController : UIViewController

    @property NSString *carName;
    @property UIImage *carImage;

@end
这意味着您可以更改当前的方法,如

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }

    cell.textLabel.text = [self titleForRow:indexPath.row];
    cell.imageView.image = [self.imageForRow:indexPath.row];

    return cell;
}
而且

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *carName = [self titleForRow:indexPath.row];
    UIImage *carImage = [self imageForRow:indexPath.row];        

    // Navigation logic may go here. Create and push another view controller.

    // OMG! Fix the naming conventions. Class names should start with uppercase letters.
    carSelectViewController *carDetailViewController = [[carSelectViewController alloc] initWithNibName:@"carSelectViewController" bundle:nil];

    // These won't work until you do the next bit.
    carDetailViewController.carName = carName;
    carDetailViewController.carImage = carImage;

    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController: carDetailViewController animated:YES];    
}
接下来,向“详细视图控制器”添加属性,如下所示

- (UIImage *)imageForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
    } else if (Carint == 1) {
        return [UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
    } else if (Carint == 2) {
        return [UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
    }

    return nil;
}
- (NSString *)titleForRow:(NSUInteger)row
{
    if (Carint == 0) {
        return [Hatchback objectAtIndex:indexPath.row];
    } else if (Carint == 1) {
        return [SUV objectAtIndex:indexPath.row];
    } else if (Carint == 2) {
        return [Sedan objectAtIndex:indexPath.row];
    }

    return nil;
}
@interface carSelectViewController : UIViewController

    @property NSString *carName;
    @property UIImage *carImage;

@end
然后,这些将由上一个视图控制器进行设置

最后,使用这些属性的值来显示数据

- (void)viewDidLoad
{
    self.title = self.carName;
    imageView.image = self.carImage;

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
关于这方面的更多信息,特别是关于前三点的工作,你可以在这里看到


我尝试了使用nsdictionaries将图像和文本的密钥对值一起显示的方法,以使代码更简单(我猜)。在下一个视图中同时显示文本和图像也符合应用程序的要求。

您应该编辑此问题以使其更清晰,并显示相关代码——我在这里没有看到任何表视图代码,或者您试图将图像传递给下一个控制器。我在手机atm上,但您似乎已将
imageForRow
的代码放入
cellForRow的代码中…
。这是一个方法实现,所以不应该在另一个方法实现中。好的,现在我回到笔记本电脑上,我可以看到你在这里做了一些非常奇怪的事情。您似乎已将代码粘贴到代码的随机位中。查看我的
cellforrowatinexpath
code。大约有10行代码。我已经删除了所有的
Carint
内容。该
Carint
只能在新函数中引用。另外,您还将函数放入了
cellForRow
等中。。。只要检查一下你的代码。对于我在回答中写的每个方法,只需删除整个方法并粘贴我的方法即可。不,您不应该在视图中存储数据。为什么不能。?为了简单地发送图像,您可以将该图像发送到下一个viewController。通过它的属性,你可以用代码来解释!我的意思是编辑什么和在哪里编辑?让我解释一下我的代码的作用:@Neelang ok,将编辑我的答案。我花了大约15分钟来编辑你的问题,以完全理解发生了什么。确保正确格式化代码。:)非常抱歉,给您带来了麻烦。但由于我不熟悉这个概念,请尝试理解并帮助我理解格式化代码的概念。谢谢&抱歉。我确实按照您的建议进行了更改,但存在一个问题:我按照您的建议进行了更改,但存在一个问题:我无法将“carselectviewcontroller”更改为“nsobject”,因为它是uiviewcontroller类型。其次,您的代码给出了3个错误:1.使用未声明的标识符imageForRow(在detailViewController中)。2.三,。cardetailViewController没有可见接口声明选择器imageForRow。(在声明*carname、*carImage的行中)。这与我的答案相同,只是它需要重构。你有短语
if(Carint==1)。。。如果(Carint==2)。。。如果(Carint==3)
多次,并且每次都在做相同的事情。(即,仅获取图像或文本)。如果你把这些重构成不同的方法,那么你就减少了行数,你就有了我的解决方案。再次感谢兄弟,我需要你的帮助,在我的另一个问题中,我在这一点上接近成功,但有点小问题,这是链接。""