2012-09-24 61 views
0

我想在每個部分實現4個單元格的表視圖,部分的數目也是2.我在每個部分的最後兩行添加了buy按鈕,使其隱藏,直到未選中行。當我點擊第一部分的最後一行時,出現購買按鈕。但是,只要我點擊第二部分(最後一部分)的最後一行,就不會出現。這是我的代碼。UIButton不會出現在UItableView的最後一節的最後一行

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

static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
    reuseIdentifier:CellIdentifier]; 

// some code for cell properties 

    if(indexPath.section==0) 
    { 
     if(indexPath.row==2) 
     {     
      //some code for property of buy button1 

      cell.accessoryType = UITableViewCellAccessoryNone; 
      cell.accessoryView = buyButton1; 
      [buyButton1 setHidden:YES]; 
     } 
     if(indexPath.row==3) 
     {        
      //some code for property of buy button2 
      cell.accessoryType = UITableViewCellAccessoryNone; 
      cell.accessoryView = buyButton2; 
      [buyButton2 setHidden:YES]; 
     } 
    }  

    if(indexPath.section==1) 
    { 
     if(indexPath.row==2) 
     { 

      //some code for property of buy button3 
      [cell addSubview:buyButton3]; 
      [buyButton3 setHidden:TRUE]; 

     } 
     if(indexPath.row==3) 
     { 

      buyButton4= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      buyButton4.frame = CGRectMake(194, 4, 70, 37); 
      [buyButton4 setTitle:@"Buy" forState:UIControlStateNormal]; 

      [buyButton4 addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
      buyButton4.tag = 4; 
      [cell addSubview:buyButton4]; 
      [buyButton4 setHidden:TRUE]; 

     } 
    } 

} 
if(indexPath.section == 0) 
{ 
    cell.textLabel.text = [privateArray objectAtIndex:indexPath.row]; 
}  
else 
{ 
    cell.textLabel.text = [workArray objectAtIndex:indexPath.row]; 

} 
return cell; 
} 

這裏是我行的選擇代碼

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

[tableViewForOccasion deselectRowAtIndexPath:indexPath animated:NO]; 

NSArray *visibleCells = [tableViewForOccasion visibleCells]; 

for (UITableViewCell *aCell in visibleCells) 
{ 
    aCell.accessoryType = UITableViewCellAccessoryNone; 
} 
if(indexPath.section==1) 
{ 
    if(indexPath.row==3) 
    { 
    [buyButton4 setHidden:FALSE]; 
    } 
} 
} 

enter image description here

回答

2

,因爲你是在第二部分講的最後一排,然後你的檢查條件是錯誤的

if(indexPath.section==1) 
{ 
    if(indexPath.row==3) // you have mistake in this part where you had if(indexPath.row==2) 
    { 
    [buyButton4 setHidden:FALSE]; 
    } 
} 
+0

ooops!對不起我的錯誤,它的if(indexPath.row == 3)但我粘貼在這裏是錯誤:) – Ketan

0
I think you forgot the `buyButton4` allocation and `setHidden = FALSE`. 

//Replace Your Code 

if(indexPath.row==3) 
        { 
     UIButton * buyButton4 =[[UIButton alloc] init]; 
            buyButton4= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
            buyButton4.frame = CGRectMake(194, 4, 70, 37); 
            [buyButton4 setTitle:@"Buy" forState:UIControlStateNormal]; 

            [buyButton4 addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
            buyButton4.tag = 4; 
            [cell addSubview:buyButton4]; 
            [buyButton4 setHidden:FALSE]; 

        } 
+0

在哪裏分配?我首先在viewdidload中分配,但沒有工作比我加入cellForRowatIndex ..沒有運氣。 :( – Ketan

+0

檢查我的新帖子:) – Rushabh

+0

但我想[buyButton4 setHidden:TRUE];當單元格被創建並且點擊行時應該顯示購買按鈕。 – Ketan

相關問題