2012-07-09 169 views
1

我已經實現了異步圖像視圖,以便在自定義單元格視圖中加載圖像,但圖像在單元格上顯示爲很小。所以我需要在點擊圖像視圖時顯示該圖像的放大形式。我怎樣才能做到這一點?在iPhone中單擊圖像視圖時放大顯示圖像

這是我的代碼:

if (cell == nil) { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    //create new cell 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    //common settings 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
    cell.layer.cornerRadius = 10; 
    cell.layer.masksToBounds = YES; 

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit; 
    cell.imageView.frame = CGRectMake(10.0f, 10.0f, 60.0f, 44.0f); 
    cell.imageView.backgroundColor = [UIColor colorWithRed:73 green:73 blue:73 alpha:1]; 

    cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; 
    imgBtn=[cell imageView]; 
    objectAtIndex:indexPath.row]objectForKey:@"Name"]; 
    cell.imageView.imageURL = [NSURL URLWithString:[[detailsList objectAtIndex:indexPath.row]objectForKey:@"image_url"]]; 
} 

我想觸摸到細胞內的圖像視圖放大圖像。有任何想法嗎?提前致謝。

+0

現在工作嗎? – 2012-07-10 09:45:16

+0

@Simha.hb s bose .. :) – 2012-07-10 09:51:19

回答

1

嘗試使用一個UIButton:

UIButton * b = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f , cell.imageView.frame.size.width, cell.imageView.frame.size.height)]; 
b.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
[b addTarget:self action:@selector(changeSize:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.imageView addSubview:b]; 

-(IBAction)changeSize:(UIButton *)sender 
{ 
    UIImageView * imageView = (UIImageView *)[sender superview]; 
    imageView.frame = //Your larger frame goes here 
} 
+0

我添加了像這樣的btn,但它沒有調用更改尺寸方法 – 2012-07-09 11:33:49

+0

確保cell.imageView.userInteractionEnabled = YES – llama591 2012-07-09 12:14:41

+0

其工作..但圖像顯示爲在兩側裁剪。 – 2012-07-09 12:36:00

3

可以按如下方式與手勢識別添加圖像: -

UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];   
tapped.numberOfTapsRequired = 1; 
[image setUserInteractionEnabled:YES]; 
image.tag = indexPath.row; 
[image addGestureRecognizer:tapped]; 
[cell.contentView addSubview:image]; 

而且你imagetapped功能將是: -

- (void)imageTapped:(UITapGestureRecognizer *)gesture 
{ 
    UIImageView *img = (UIImageView *)[gesture view]; 
    BigimgView.image = img.image 
    [self.view addSubview:BigimgView]; 
    UITableViewCell *cell = [MyTableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:img.tag]]; 

} 

和BigimgView可以IBOutlet中大尺寸的ImageView的。

+0

點擊圖片視圖是不是調用圖片點擊的方法.wat可能是什麼原因? – 2012-07-09 11:49:22

+0

@alpz我修改了我的答案讓用戶交互啓用imageview。 – Dhawal 2012-07-09 15:37:26

+0

,但更大的圖像始終顯示在相同的位置。我需要在表格視圖本身的特定單元格上顯示放大的圖像。因此,單元格的標識符也是必需的。 – 2012-07-10 04:56:48

1

當您點擊imageView時,您想收到一條消息嗎? 一個可能的解決方案是創建一個自定義的UITableViewCell,也許在一個自己的xib文件中。

然後你的問題將是使你的自定義UITableViewCell點擊UIImageView。 請看看這個問題:here

一個簡單的解決方案是使用UIButton,而不是UIImageView。你可以設置一個圖像/ backgroundImage到UIButton。

1

在你的自定義單元格添加按鈕。

@interface custom : UITableViewCell 
{ 
    UIButton *button1 
} 

然後在xib中添加按鈕,然後連接到該按鈕。

來到tableview數據源方法。

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

    // Dequeue or create a cell of the appropriate type. 
    /// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    custom *cell=(custom *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     NSArray *topView=[[NSBundle mainBundle]loadNibNamed:@"custom" owner:self options:nil]; 

     for(id currentobject in topView) 
     { 
      if ([currentobject isKindOfClass:[UITableViewCell class]]) 
      { 
       cell=(custom *)currentobject; 
       //cell.backgroundView.backgroundColor=[UIColor clearColor]; 

       break; 
      } 
     } 
    } 
    // 0.0f, 0.0f , cell.imageView.frame.size.width,  cell.imageView.frame.size.height 
    [cell.button1 addTarget:self action:@selector(changeSize) forControlEvents:UIControlEventTouchUpInside]; 
} 

現在點擊button.it會去那個方法。,。,試試吧。,。,

-(IBAction)changeSize:(UIButton *)sender 
{ 
    UIImageView * imageView = (UIImageView *)[sender superview]; 
    imageView.frame = //Your larger frame goes here 
} 

ü需要給背景圖像cell.button1 它可能幫助你。

我在學習,