2012-04-19 5 views
1

3 개의 섹션이 포함 된 그룹화 된 tableview가 있습니다. 각 섹션마다 다른 버튼을 추가하고 싶습니다. 첫 번째 섹션에는 5 개의 셀이 있고, 2 번째 섹션에는 3 개의 unbutton을 추가하고 싶습니다. 3r 번째 cell.my 코드는 다음과 같습니다.UItableview 셀에 UIButton 추가하기

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 3; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if(section == 0) 
     return [_arayfirstrow count]; 
    else if(section == 1) 
     return [_arraysecondrow count]; 
    else if(section == 2) 
     return [_arraythirdrow count]; 
} 
    // Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    // static NSUInteger nTag = 100; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.font = [UIFont fontWithName:@"Georgia"size:20.0]; 



    } 

    // Set up the cell... 
    if(indexPath.section == 0){ 
      cell.textLabel.text = [_arayfirstrow objectAtIndex:indexPath.row]; 
     NSString *imageName = [_arayfirstrowimg objectAtIndex:indexPath.row]; 
     cell.imageView.image = [UIImage imageNamed:imageName]; 

**//when i add below code for inserting button on the first section ,it successfully adds the button,but i don't need button for 1st and 5th cell..ans also it crashes when i tap the button for 2 or three time** 

    CGRect buttonRect = CGRectMake(210, 25, 65, 25); 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = buttonRect; 
     // set the button title here if it will always be the same 
     [button setTitle:@"Action" forState:UIControlStateNormal]; 
     button.tag = 1; 
     [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 

     [cell.contentView insertSubview:button atIndex:0]; 



     // [usernamebutton release]; 

    }else if(indexPath.section == 1){ 
     cell.textLabel.text = [_arraysecondrow objectAtIndex:indexPath.row]; 
    } 
    else if(indexPath.section == 2) 
    { 
     cell.textLabel.text = [_arraythirdrow objectAtIndex:indexPath.row]; 

    } 
    return cell; 
} 

해결 방법은 없습니다. 사전에 감사합니다.

+0

., – Bala

답변

2

이 같은 시도 할 수 있습니다 :

if(indexPath.section == 0) 
{ 
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3) 
    { 
     CGRect buttonRect = CGRectMake(210, 25, 65, 25); 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     button.frame = buttonRect; 
     // set the button title here if it will always be the same 
     [button setTitle:@"Action" forState:UIControlStateNormal]; 
     button.tag = 1; 
     [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubView:button atIndex:0]; 
    } 
} 

당신이 방법 myAction:을 정의 있나요? 그렇지 않은 경우 응용 프로그램이 손상 될 수 있습니다. 당신이 다른 무언가를 얻을 수 있습니다 이동하면

+0

작품을 작동 병아리 할 수 ​​있습니다. – stackiphone

+0

:) 도움을 드릴 perfectt – Alok

1

첫째, 이것은 매우 간단합니다 :

, 프로젝트에있는 UITableViewCell의 하위 클래스를 추가 헤더를 삽입과 같이 대리자 방법에 셀을 추가

static NSString *CellIdentifier = @"SpecialCell"; 
// static NSUInteger nTag = 100; 
YourTableViewCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (specialCell == nil) { 
    specialCell = [[[YourTableViewCellClass alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

를 새로운 자신의 셀에 당신이

[self addSubview:button]; 

이 당신의 특별한 세포, 첫 번째 섹션에 2, 3, 4 셀의 셀입니다 함께 원하는대로 클래스는 버튼과 뷰를 추가 할 수 있습니다.

이미 설명한 것처럼 위와 같은 방법으로 표준 셀을 초기화하십시오.

if(indexPath.section == 0){ 
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3){ 
     //add text to buttons of your specialCell 
     //add color... 
     return specialCell; 
    } else { 
     return cell; 
    } 
} 

그것을 할 것을 의미한다 방법 그냥 작은 개요 : 지금 여기에 마법을 온다. 도움이 되길 바랍니다.

+1

는 내가 그것을 감사 – stackiphone

0
if(indexPath.section == 0) 
{ 
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3) 
    {  

UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      [deletebtn setFrame:CGRectMake(170,5, 25, 25)]; 
      deletebtn.tag=indexPath.row; 
      [deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal]; 
      [deletebtn addTarget:self action:@selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside]; 
      [cell addSubview:deletebtn]; 
} 
} 
관련 문제