2012-04-19 178 views
1

我有一個包含3個部分的分組tableview,我想爲每個部分添加不同的按鈕,我在第一部分中有5個單元格,並且我想在第二個部分中只添加3個解除按鈕, 3R的第四cell.my代碼看起來像這樣添加UIButton到UItableview單元

- (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 2012-04-19 14:53:06

回答

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

作品perfectt感謝 – stackiphone 2012-04-19 14:52:27

+0

:)樂意提供幫助。 – Alok 2012-04-19 14:56:04

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]; 

這是你的特殊的細胞,你的第二,第三和第四單元在第一部分中的細胞一樣。

像你已經做的那樣,按照上面的相同方式創建一個標準單元格。 現在,這裏是魔法:

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 2012-04-19 14:29:39

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]; 
} 
} 
相關問題