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

时间:2014-01-01 23:23:20

标签: ios uitableview

的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

3 个答案:

答案 0 :(得分:1)

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

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

通过这样做,您可以将表格强烈地耦合到它正在显示的数据。我现在暂时忽略这一点。

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

第三,您应该创建一个名为Car的类。然后给班级Car两个属性nameimage,实际上你也可以给它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;
}

这意味着您可以更改当前的方法......

- (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];    
}

接下来,将属性添加到“详细视图控制器”,如下所示......

@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.
}

有关执行此操作的详细信息,尤其是前三点,您可以在此处查看...

http://www.raywenderlich.com

答案 1 :(得分:0)

**CARDETAILVIEWCONTROLLER**
- (void)viewDidLoad
{

[self showData];
[super viewDidLoad];

self.tableView.backgroundColor = [UIColor brownColor];
}
-(void) showData
{
Hatchback = [[NSMutableArray alloc]init];

SUV = [[NSMutableArray alloc]init];

Sedan = [[NSMutableArray alloc]init];


//Hatchback

[Hatchback addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Hyundai i10",@"name",@"i10.jpg",@"image",nil]];

[Hatchback addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Hyundai i20",@"name",@"i20.jpg",@"image",nil]];

[Hatchback addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Maruti Suzuki Swift",@"name",@"Swift.jpeg",@"image",nil]];

[Hatchback addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Maruti Suzuki wagonR",@"name",@"wagonR.jpg",@"image",nil]];

[Hatchback addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Honda Brio",@"name",@"brio.jpg",@"image",nil]];
[Hatchback addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Ford figo",@"name",@"figo.jpg",@"image",nil]];

//SUV

[SUV addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Tata safari storme",@"name",@"safari.jpg",@"image",nil]];


[SUV addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Mahindra scorpio",@"name",@"scorpio.jpg",@"image",nil]];

[SUV addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Mahindra xuv 500",@"name",@"xuv.jpg",@"image",nil]];

[SUV addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Renault duster",@"name",@"duster.jpg",@"image",nil]];

//Sedan

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Honda city",@"name",@"city.jpg",@"image",nil]];

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"MarutiSuzuki dzire",@"name",@"dzire.jpg",@"image",nil]];

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Hyundai verna",@"name",@"verna.jpg",@"image",nil]];

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Skoda octavia",@"name",@"octavia.jpg",@"image",nil]];

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Honda civic",@"name",@"civic.jpg",@"image",nil]];

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Honda amaze",@"name",@"amaze.jpg",@"image",nil]];

[Sedan addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Ford fiesta",@"name",@"fiesta.jpg",@"image",nil]];


}

- (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.
if (Carint ==0) {
    return [Hatchback count];
}
if (Carint ==1) {
    return [SUV count];
}
if (Carint ==2) {
    return [Sedan count];
}
[self.tableView reloadData];
}

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];

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


    if (Carint == 0)
    {
        cell.imageView.image=[UIImage imageNamed:[HatchbackImg objectAtIndex:indexPath.row]];
        cell.textLabel.text =[[Hatchback objectAtIndex:indexPath.row]objectForKey:@"name"];
    }
    if (Carint == 1)
    {
        cell.imageView.image=[UIImage imageNamed:[SUVImg objectAtIndex:indexPath.row]];
        cell.textLabel.text = [[SUV objectAtIndex:indexPath.row] objectForKey:@"name"];
    }

    if (Carint == 2)
    {
        cell.imageView.image=[UIImage imageNamed:[SedanImg objectAtIndex:indexPath.row]];
        cell.textLabel.text = [[Sedan objectAtIndex:indexPath.row ]objectForKey:@"name"];
    }

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}

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
{
carSelectViewController *car = [[carSelectViewController alloc]initWithNibName:@"carSelectViewController" bundle:nil];

if (Carint == 0)
{
    car.carImageString = [[NSString alloc]initWithString:[[Hatchback objectAtIndex:indexPath.row]objectForKey:@"image"]];
    car.carLabelString = [[NSString alloc]initWithString:[[Hatchback objectAtIndex:indexPath.row]objectForKey:@"image"]];
    car.title = [[Hatchback objectAtIndex:indexPath.row]objectForKey:@"name" ];

}
else if (Carint ==1)
{
    car.carImageString = [[NSString alloc]initWithString:[[SUV objectAtIndex:indexPath.row]objectForKey:@"image"]];
    car.carLabelString = [[NSString alloc]initWithString:[[SUV objectAtIndex:indexPath.row]objectForKey:@"image"]];

    car.title = [[SUV objectAtIndex:indexPath.row]objectForKey:@"name" ];

}
else if (Carint ==2)
{
    car.carImageString = [[NSString alloc]initWithString:[[Sedan objectAtIndex:indexPath.row]objectForKey:@"image"]];
    car.carLabelString = [[NSString alloc]initWithString:[[Sedan objectAtIndex:indexPath.row]objectForKey:@"image"]];

    car.title = [[Sedan objectAtIndex:indexPath.row]objectForKey:@"name" ];

}
[self.navigationController pushViewController:car animated:YES];
}
@end

**CARSELECTVIEWCONTROLLER


@synthesize carImageString,carLabelString;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}
NSString *localImgName;

- (void)viewDidLoad
{
carImage.image = [UIImage imageNamed:carImageString];
carLabel.text = carLabelString;
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

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

- (IBAction)btnBook:(id)sender {
carBookViewController *Book = [[carBookViewController alloc]initWithNibName:@"carBookViewController" bundle:nil];
[self.navigationController pushViewController: Book animated:YES];
}
@end

我尝试过将nsdictionaries用于图像的密钥对值的方法。一起编写代码以使代码更简单(我猜)。它适用于应用程序的要求以及显示文本和&在下一个视图上一起拍摄。

答案 2 :(得分:-1)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NextViewController *viewcontroller=[NextViewController alloc]init];
viewcontroller.image=[tableView cellForRowAtIndexPath:indexPath].imageView.image;

}

然后按或显示此视图Controller以显示。

viewcontroller.image是你下一堂课的cusotm属性  然后在viewdidload方法

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