0

我正在尝试在每个部分中使用 4 个单元格来实现表格视图,并且部分的数量也是 2。我在每个部分的最后两行中添加了购买按钮,并使其隐藏,直到未选择行。当我点击第一部分的最后一行时,会出现购买按钮。但是,只要我点击第二部分(最后一部分)的最后一行,它就不会出现。这是我的代码。

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

在此处输入图像描述

4

2 回答 2

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];
   }
}
于 2012-09-24T07:24:26.880 回答
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];

        }
于 2012-09-24T07:29:35.173 回答