2012-02-09 16 views
0

我有一個UITableViewController(稱爲細節)。在第1行的tableView中,我有另一個水平旋轉的UITableView。該tableview中的每個單元格都是一個定製的UITableViewCell(稱爲DetailsHorizo​​ntalPhotoCell),它顯示帶有圖像的按鈕。當按下照片(按鈕)時,我很難採取行動。需要採取UIButton按下是在與自定義UITableViewCell嵌套TableView的行動

如果我在DetailsHorizo​​ntalPhotoCell.h/.m文件中有IBAction代碼,調用IBAction對我來說不是問題,但是我需要在按鈕按下時呈現模態VC。該代碼不屬於和/或在tableViewCell中工作 - 它需要位於控制器(詳細信息)中。這就是說我無法弄清楚如何編寫代碼。這是我迄今爲止所擁有的。

Details.m:

if((indexPath.row == 1) && ([detailsObject_.photo count] > 0)) 
{ 
    NSLog(@"** In Photo Section **"); 

    static NSString *CellIdentifier = @"cellPhoto"; 
    DetailsHorizontalPhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[DetailsHorizontalPhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2); 
    cell.horizontalTableView.transform = rotateTable; 

    NSArray *photoArray = [[NSArray alloc] initWithArray:detailsObject_.photo]; 

    cell.horizontalTableView.frame = CGRectMake(0, 0, cell.horizontalTableView.frame.size.width, cell.horizontalTableView.frame.size.height); 
    cell.contentArray = [NSArray arrayWithArray:photoArray]; 
    cell.horizontalTableView.allowsSelection = YES; 

    return cell; 
} 

DetailsHorizo​​ntalPhotoCell.m:

@synthesize horizontalTableView = horizontalTableView_; 
@synthesize contentArray = contentArray_; 
@synthesize imageButton = imageButton_; 

// *** a bunch of code which is not relevant to this question snipped out here... 


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

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    } 

    for(UIButton *button in cell.subviews) 
    { 
     [button removeFromSuperview]; 
    } 

    // create image and button 
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]]; 
    self.imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)]; 

    // setup the button 
    [imageButton_ setImage:image forState:UIControlStateNormal]; 
    imageButton_.layer.masksToBounds = YES; 
    imageButton_.layer.cornerRadius = 5.0; 
    imageButton_.layer.borderWidth = 1.0; 
    imageButton_.layer.borderColor = [[UIColor grayColor] CGColor]; 

    // rotate the button 
    CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2); 
    imageButton_.transform = rotateButton; 

    // this detects the click of each photo and triggers the IBAction 
    [imageButton_ addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside]; 

    /// do more stuff yada yada yada... <snipped code> and return cell; 
} 


// the Action (which I know should NOT be in this UITableViewCell class) 
- (IBAction)photoButton:(id)sender 
{ 
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView]; 
    NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint]; 

    NSLog(@"Image clicked, index: %d", hitIndex.row); 

    // presentModalViewController here - but you can't do that from here! Must be in the Details controller 
} 

所以,知道我不能從小區進行presentModalViewController我嘗試在代碼進入詳細信息類(下)

將IBAction添加到Details.h和.m

- (IBAction)photoButton:(id)sender 
{ 
    DetailsHorizontalPhotoCell *cell = [[DetailsHorizontalPhotoCell alloc] init]; 

    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:cell.horizontalTableView]; 
    NSIndexPath *hitIndex = [cell.horizontalTableView indexPathForRowAtPoint:hitPoint]; 

    NSLog(@"Image clicked, index: %d", hitIndex.row); 
} 

並將單擊事件添加到Details cellForRowAtIndexPath方法中的照片按鈕。

// Blaaahh! Tried a million combinations of something like below but cannot get it to work... 
//[cell.imageButton addTarget:cell.horizontalTableView action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside]; 

PS - 我使用的IOS5,Xcode中4.2瓦特/ ARC

回答

1

首先,沒有必要給每個顯示單元時添加的按鈕。您只需在創建新單元格時添加單元格的子視圖。這樣你會獲得更好的滾動性能。

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

    UITableViewCell *cell = [self.horizontalTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

     // Add image button to cell 
     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)]; 
     // set tag so you can get the button later 
     button.tag = 1021; 
     [button addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside] 
     // more button config 
     [cell.contentView addSubView:button]; 
    } 
    // get image button 
    UIButton *button = [cell.contentView viewWithTag:1021]; 
    // configure image button 
    UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]]; 
    [button setImage:image forState:UIControlStateNormal]; 
    return cell; 
} 

接下來,您可以使用按鈕(= sender)上的使用superview返回到UITableViewCell。使用單元格,您可以獲取該單元格的indexPath。

- (IBAction)photoButton:(id)sender 
{ 
    UIView *contentView = [sender superview]; 
    UITableViewCell *cell = [contentView superview]; 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
} 
+0

感謝您的回答。但我有兩個問題。 1)這行'UIButton * button = [cell.contentView viewWithTag:1021];'產生一個編譯器警告'不兼容的指針類型用'UIView *'類型的表達式初始化'UIButton * __ strong' - 關於如何解析的任何建議那? 2)關於'tag 1021',1021只是你選擇的一個隨機數?是否有我應該知道和/或避免使用的保留號碼或一系列號碼? – ElasticThoughts 2012-02-11 00:15:03

+0

我擺脫了編譯器的警告,通過將它轉換爲類型UIButton類似於這樣'UIButton * imageButton =(UIButton *)[cell.contentView viewWithTag:1021];',這是安全的和/或正確的方法來糾正問題我有?提前致謝。 – ElasticThoughts 2012-02-11 16:44:14

+0

既然你知道它是一個UIButton,就像開發者一樣,這樣做是安全的做法。 UIButton是UIView的子類,所以在調用'viewWithTag:'時你仍然在技術上得到一個UIView。你甚至可以把返回的對象放到UIView *中,但是你不會像'setImage:'那樣獲得所有那些漂亮的UIButton方法。 – Austin 2013-09-26 19:55:24

0

你可以添加一個特定的標籤或訪問與[按鈕上海華]上海華]到UITableView的

1

您是否無法檢測按下按鈕或者是你在有從麻煩presentModalViewController工作的tableview?如果您遇到問題檢測按鈕按下,那麼你可能只需要:

[button setExclusiveTouch:YES]; 

如果你有麻煩了,因爲你不能使用presentModalViewController從的tableView內,則喲可能能夠使用:

[[NSNotificationCenter defaultCenter] addObserver: selector: name: object:]; 

要通知你的「細節類」使用presentModalViewController

相關問題