2013-02-08 26 views
1

我想顯示所選單元格的子視圖。所選單元格應該在雙擊單元格時展開,並且應在該展開區域中顯示子視圖。我能夠在單元格的展開區域中顯示的子視圖中顯示圖像。但是當我在雙擊單元格後滾動UITableView時,子視圖中顯示的圖像正在消失。如何在雙擊UITableViewCell時顯示子視圖?

請建議我.............

擴大電池:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (selectedRow && indexPath.row == tappedRow && (tapCount == 2 || tapCount ==3)) 
     return 210; 

    return 62; 
} 

代碼顯示子視圖對於選擇的行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    //UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ChooseAPlayer"]; 
    if(tapCount == 1 && tappedRow == indexPath.row){ 

     tapCount = tapCount + 1; 
     imgView.hidden =YES; 

     selectedRow = [self.tableView indexPathForSelectedRow]; 
     [self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone]; 
     imgView.hidden = NO; 
     button.frame = CGRectMake(245,18,image.size.width, image.size.height); 
     // cell.accessoryView.hidden = YES; 
     [UIView commitAnimations]; 

    } 
    else if (tapCount == 0){ 

     tapCount = tapCount +1; 
     tappedRow = indexPath.row; 
    } 
    else if (tapCount ==2 && tappedRow == indexPath.row){ 

     tapCount = tapCount + 1; 
     imgView.hidden =YES; 

     selectedRow = [self.tableView indexPathForSelectedRow]; 
     [self.tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationNone]; 
     imgView.hidden = NO; 
     button.frame = CGRectMake(245,18,image.size.width, image.size.height); 
     // cell.accessoryView.hidden = YES; 
     [UIView commitAnimations]; 
    } 
    else if (tapCount ==3 && tappedRow == indexPath.row){ 
     tapCount = 0; 
     //cell.accessoryView.hidden = NO; 
     imgView.hidden = YES; 

     [self.tableView setRowHeight:62]; 
     [self.tableView beginUpdates]; 
     [self.tableView endUpdates]; 
     [imgView release]; 
    } 
    else if (tappedRow != indexPath.row){ 
     tapCount = 1; 
     tappedRow = indexPath.row; 
     imgView.hidden = YES; 

     [imgView release]; 
    } 

} 




- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ChooseAPlayer"]; 


    imgView.hidden = YES; 


    if (indexPath.row == tappedRow && (tapCount == 2 || tapCount ==3)) 
    { 
     imgView.tag = indexPath.row; 
     imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320,150)]; 
     imgView.backgroundColor = [UIColor clearColor]; 
     [imgView setImage:[UIImage imageNamed:@"Playersdetails.png"]]; 
     [cell.contentView addSubview:imgView]; 
     imgView.hidden= NO; 
    } 


    UILabel *lblName = (UILabel *)[cell viewWithTag:101]; 
    [lblName setText:[inputPlayersList objectAtIndex:[indexPath row]]]; 
    UILabel *Teams = (UILabel *)[cell viewWithTag:103]; 
    [Teams setText:[inputPlayersTeams objectAtIndex:[indexPath row]]]; 

    UILabel *AwayTeams = (UILabel *)[cell viewWithTag:104]; 
    [AwayTeams setText:[inputPlayersAwayteams objectAtIndex:[indexPath row]]]; 

    UILabel *PlayersPrice = (UILabel *)[cell viewWithTag:105]; 
    [PlayersPrice setText:[inputPlayersPrice objectAtIndex:[indexPath row]]]; 



    image = [UIImage imageNamed:@"Select_white.png"]; 
         button = [UIButton buttonWithType:UIButtonTypeCustom]; 
         CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); 
         button.frame = frame; 
         [button setBackgroundImage:image forState:UIControlStateNormal]; 

         [button addTarget:self action:@selector(accessoryButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
         button.backgroundColor = [UIColor clearColor]; 
         cell.accessoryView = button; 

    return cell; 
} 

回答

1

試試這個在cellForRowAtIndexPath

// imgView.hidden = YES; //comment this line 

if (indexPath.row == tappedRow && (tapCount == 2 || tapCount ==3)) 
      { 
       imgView.tag = indexPath.row; 
       imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320,150)]; 
       imgView.backgroundColor = [UIColor clearColor]; 
       [imgView setImage:[UIImage imageNamed:@"Playersdetails.png"]]; 
       [cell.contentView addSubview:imgView]; 
       imgView.hidden= NO; 
      } 
      else 
      { 
       imgView.hidden = YES; 

      } 
1

要獲取的UITableView單/雙擊動作,請遵循以下步驟:

創建CustomTableViewCell,子類UITableViewCell的。並覆蓋上面提到的方法。

  #import "CustomTableViewCell.h" 


       @implementation CustomTableViewCell 

       - (void)dealloc { 
        [super dealloc]; 
       } 

       - (void)drawRect:(CGRect)rect { 
        CGContextRef ctx = UIGraphicsGetCurrentContext(); 
        // Use the same color and width as the default cell separator for now 
        CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0); 
        CGContextSetLineWidth(ctx, 0.5); 

        for (int i = 0; i < [columns count]; i++) { 
         CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue]; 
         CGContextMoveToPoint(ctx, f, 0); 
         CGContextAddLineToPoint(ctx, f, self.bounds.size.height); 
        } 

        CGContextStrokePath(ctx); 

        [super drawRect:rect]; 
       } 

       - (void)addColumn:(CGFloat)position { 
        [columns addObject:[NSNumber numberWithFloat:position]]; 
       } 


       - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
        UITouch *aTouch = [touches anyObject]; 
        NSLog(@"touchesBegan tapCount = %d", [aTouch tapCount]); 
        //// 
         imgView.tag = indexPath.row; 
       imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, 320,150)]; 
       imgView.backgroundColor = [UIColor clearColor]; 
       [imgView setImage:[UIImage imageNamed:@"Playersdetails.png"]]; 
       [cell.contentView addSubview:imgView]; 
       imgView.hidden= NO; 

       ///// 
      } 


      - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { 
       UITouch *aTouch = [touches anyObject]; 
       NSLog(@"touchesCancelled tapCount = %d", [aTouch tapCount]); 
      } 




    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
      UITouch *aTouch = [touches anyObject]; 
      NSLog(@"touchesEnded tapCount = %d", [aTouch tapCount]); 
     } 


     - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
      UITouch *aTouch = [touches anyObject]; 
      NSLog(@"touchesMoved tapCount = %d", [aTouch tapCount]); 
     } 

2.UITableView'scellForRowAtIndexPath方法中,添加的CustomTableViewCell代替UITableViewCell

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

     NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row]; 

     CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

     if (cell == nil) { 
      cell = [[[CustomTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
     } 

     UILabel *regNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, 150, 15)]; 
     //regNoLabel.text = [[self.resultArray objectAtIndex:indexPath.section] valueForKey:@"Ent_Regn_Nbr"]; 
     regNoLabel.text = @"Apple"; 
     regNoLabel.font = [UIFont systemFontOfSize:12.0]; 
     [cell addSubview:regNoLabel]; 
     [regNoLabel release]; 
      return cell; 

    } 
相關問題