detailtableview控制器从tableview推送图像

时间:2014-01-21 04:27:16

标签: ios objective-c uitableview

嗨我正在尝试查看图像和描述表格tableview到detailviewcontroller,但不是我无法得到它。我正在获取图像表单服务器,我已经存储了图像URL并使用json和php编码我使用NSURLConnectionDelegate查看图像和描述在tableview中获取图像URL。

我尝试了很多方法来查看tableview中的图像但是获取图像。

detailview controller.h文件编码

#import <UIKit/UIKit.h>
#import "image.h"
@class image;
@interface viewdetailpoliticalViewController : UIViewController<NSURLConnectionDelegate>
{
  NSURLConnection *connection;
}


    @property (strong,nonatomic) NSString *value;
    @property (strong,nonatomic) UIImage *imm;

    @property (strong, nonatomic) IBOutlet UIImageView *imageview;
    @property (strong, nonatomic) IBOutlet UILabel *dcp
    @property (strong, nonatomic) NSMutableData *responseData;

 -(void)setDataSource:(image *)inImageOb;
  @end

这是我的detailview controller.m文件编码

  #import "viewdetailpoliticalViewController.h"
  #import "image.h"
  @interface viewdetailpoliticalViewController ()

  @end

  @implementation viewdetailpoliticalViewController
  @synthesize imageview,dcp;
  @synthesize value,imm;

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

  - (void)viewDidLoad
 {
    [super viewDidLoad];

    self.imageview.image = self.imm;
    self.dcp.text = self.value;
// Do any additional setup after loading the view.
 }

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


 -(void)setDataSource:(image *)inImageOb
 {
   self.value = inImageOb.desp;


   NSURL *url = [NSURL URLWithString:inImageOb.img];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
   self.responseData = [[NSMutableData alloc] init];

 }


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
{
     [self.responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
    UIImage *image = [UIImage imageWithData:self.responseData];
    self.imm = image;
}


@end

这是我的tableview控制器m文件编码:

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [self performSegueWithIdentifier:@"Detailsegue" sender:indexPath];
     }
   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:@"Detailsegue"]) {

    viewdetailpoliticalViewController *detailvc = (viewdetailpoliticalViewController *)segue.destinationViewController;

     NSIndexPath *indexPath =[self.mytableview indexPathForSelectedRow];

    [detailvc setDataSource:[imgevery objectAtIndex:indexPath.row]];


  }
}

这是我用来使用json

获取数据的代码
  -(void) retrieveData
{
   NSURL * url = [NSURL URLWithString:getDataURL];
   NSData * data = [NSData dataWithContentsOfURL:url];

   json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];



   imgevery =[[NSMutableArray alloc]init];

for (int i=0; i<json.count; i++) {

    NSString * dd = [[json objectAtIndex:i]objectForKey:@"imgp"];
    NSString * plae =[[json objectAtIndex:i]objectForKey:@"disp"];
    image *myimg =[[image alloc]initWithimg:dd anddesp:plae];

    [imgevery addObject:myimg];

 }

  [self.mytableview reloadData];

}

这是我用于tableview单元格的代码

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *cellIdentifier =@"Cell";

imgpoliticalCell *cell =(imgpoliticalCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell== nil) {


    cell = [[imgpoliticalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell setDataSource:[imgevery objectAtIndex:indexPath.row]];

 // cell.thumbImageView.image = _img;


   return cell;
 }

1 个答案:

答案 0 :(得分:0)

发现问题是你通过Setimage Source传递数据

你在viewDidload本身之后设置图像 - (void)setDataSource:(image *)inImageOb只有viewDidload()会运行但你在viewDidLoad()本身中将图像分配给imageview

- (void)viewDidLoad
 {
    [super viewDidLoad];

    self.imageview.image = self.imm;
    self.dcp.text = self.value;
// Do any additional setup after loading the view.
 }
  • (void)connectionDidFinishLoading将在ViewDidLoad()
  • 之后运行

你要设置Image in - (void)connectionDidFinishLoading:(NSURLConnection *)连接;只有在连接didFinish时会在最后运行才会这样做

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
    imm = [UIImage imageWithData:self.responseData];
    self.dcp.text = value;
    self.imageview.image = imm;

}

希望现在有效......