2011-06-06 22 views
0

我有一個UITableView,併爲每個單元填充信息,詳細信息和圖像。我也有它,所以當一個單元格被選中時,它將帶給用戶一個詳細的視圖。我試圖完成的是如何區分用戶單擊單元格或單元格中的圖像時的差異。在UITableView中選定的UIImage

我想顯示一個小的子視圖,當用戶單擊單元格中的圖像時,小的子視圖將保留在原始框架中,但會給用戶其他可與其交互的東西。

我看了四周,似乎甚至找不到一個簡單的解釋或某處開始。它是由Facebook和Twitter等應用程序完成的,在表格視圖中您可以看到用戶個人資料圖片,您可以單擊此圖標而不是實際的單元格....

任何信息都將非常感謝!

謝謝@Nekno我已經開始嘗試實施這種方法,並且如果您不介意清理或給出您的意見,請回答幾個問題。

這是我迄今寫道

// Here we use the new provided setImageWithURL: method to load the web image with  
//SDWebImageManager 
[cell.imageView setImageWithURL:[NSURL URLWithString:[(Tweet*)[avatarsURL 
objectAtIndex:indexPath.row]avatarURL]] 
       placeholderImage:[UIImage imageNamed:@"avatar.png"]]; 




//create the button 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setBackgroundImage:[NSURL URLWithString:[(Tweet*)[avatarsURL 
objectAtIndex:indexPath.row]avatarURL]] forState:UIControlStateNormal]; 


//the button should be as big as the image in tableView 
[button setFrame:CGRectMake(10, 10, 45, 45)]; 


//set the type of action for the button 
[button addTarget:self action:@selector(imageSubViewAction:) 
forControlEvents:UIControlEventTouchUpInside]; 


//more stuff 

return cell } 

然後我用這個動作,我在@選擇指定的上述

-(IBAction)imageSubViewAction:(id)sender{ 


    [UIView beginAnimations:@"animateView" context:nil]; 
    [UIView setAnimationDuration:0.5]; 

    CGRect viewFrame = [subView frame]; 
    viewFrame.origin.x += -300; 
    subView.frame = viewFrame;   

    subView.alpha = 1.0; 

    [self.view addSubview:subView]; 
    [UIView commitAnimations]; 

所以我知道我必須行動增加首先解僱subView,但首先要做的事情。

目前爲止,這似乎對細胞沒有任何影響,我不確定接下來要採取哪些步驟才能使其發揮功能。

手勢方法是否完全禁用了didselectindexrow的功能?因爲我想有兩個。

看起來好像這種方法只需在單元格內添加一個按鈕,然後給該按鈕一個動作就可以工作,但我有點失落,無法繼續我的旅程。

再次感謝我非常感謝幫助!

} 
+0

現在看到您的代碼,我看到您正在使用內置的UITableViewCell.imageView。我以爲你有一個自定義的單元格。在你的情況下,我只是讓你的UIButton的backgroundColor設置爲'[UIColor clearColor]'而不是背景圖像。我認爲你只需要將你的按鈕作爲子視圖添加到單元格中,例如'[cell addSubview:button]'和[cell bringSubviewToFront:button]'。這樣,圖片的延遲加載和清晰的按鈕就位於頂部。 – nekno 2011-06-07 01:17:24

+0

謝謝,我感謝您的洞察力!我正在處理其餘的部分。只需在單元格中添加按鈕是有意義的。我已經有了一個動畫和子視圖,我只需要將它實現到單元格中......我肯定會更新這篇文章,當我確定將來爲任何其他人試圖實現這一目標時會更新這篇文章。非常感謝! – FreeAppl3 2011-06-07 01:59:33

+0

嗯,我得到它的工作......難的部分!現在我似乎無法擺脫那個麻煩的按鈕...我已經嘗試了一切,甚至採取了放置在一個圖像,所以我打開Photoshop的空白image.png並把它放在.....猜猜還是白色白色的藍色選擇... [按鈕setBackgroundImage:[UIImage imageNamed:@「noimage.png」] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor clearColor]];這些工作都沒有,我嘗試了其他幾種方法來嘗試......當像這樣簡單的事情成爲問題時,我會笑起來 – FreeAppl3 2011-06-07 03:29:55

回答

1

我認爲最簡單的方法是添加一個UIButton的子視圖,並添加圖像作爲UIButton的背景來看,無論是通過界面生成器或創建一個UIImage。

然後,您可以將事件處理程序連接到Touch Up Inside插座,該處理程序將處理按鈕水龍頭。

使用UIButton時,UITableView單元格選擇不應觸發。如果由於某種原因,當用戶觸摸按鈕時發現表格單元格被選中,則可以使用UIImageView而不是UIButton,然後將手勢識別器添加到處理圖像觸摸的UIImageView並取消表格單元格選擇。

要增加手勢識別到的UIImageView並取消觸摸了的UITableView:

注:ImageView的是一個局部變量引用您的UIImageView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //... 
    // by default, UITapGestureRecognizer will recognize just a tap. 
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; 
    gestureRecognizer.cancelsTouchesInView = YES; // cancels table view cell selection 
    [imageView addGestureRecognizer:gestureRecognizer]; 
    [gestureRecognizer release]; 
    //... 
} 

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer { 
    // get the imageView that was tapped 
    UIImageView* imageView = gestureRecognizer.view; 
    // do something else 
} 
0

這裏是我做到了這一點的人誰是尋找一個答案,你可以隨意使用!

//create the button 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[cell addSubview:button]; 
[cell bringSubviewToFront:button]; 

button.backgroundColor = [UIColor clearColor]; 


//[button setBackgroundImage:[UIImage imageNamed:@"cellbutton.png"] 
//forState:UIControlStateNormal]; //sets the image 

[button setImage:[UIImage imageNamed:@"cellButton.png"] 
forState:UIControlStateHighlighted]; //sets the highlighted image    

//the button should be as big as the image in tableView 

[button setFrame:CGRectMake(10, 25, 50, 50)]; 

//set the type of action for the button 
[button addTarget:self action:@selector(detailSubView:) 
forControlEvents:UIControlEventTouchUpInside]; 
相關問題