iPhone sdk如何在两个不同的按钮点击上添加/删除UIButton

时间:2013-01-11 08:51:19

标签: iphone uibutton hidden

我有两个单独的按钮,分别在UITableView中按升序和降序排序数组。

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

     checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        checkButton.frame = CGRectMake(540, 30, 42, 42);
        [checkButton setBackgroundImage:[[UIImage imageNamed:@"Down Arrow.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
        [checkButton addTarget:self action:@selector(descender:) forControlEvents:UIControlEventTouchUpInside];
        [checkButton setTag:200];
        [cell.contentView addSubview:checkButton];


        checkButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        checkButton1.frame = CGRectMake(600, 30, 42, 42);
        [checkButton1 setBackgroundImage:[[UIImage imageNamed:@"Up Arrow.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
        [checkButton1 addTarget:self action:@selector(ascender:) forControlEvents:UIControlEventTouchUpInside];
        [checkButton1 setTag:100];
        [cell.contentView addSubview:checkButton1];

checkButton = (UIButton *)[cell viewWithTag:200];
        if (indexPath.row == 0 && appDelegate.a == 300)
        {
            checkButton.hidden = NO;
            checkButton1.hidden = YES;
        }

        checkButton1 = (UIButton *)[cell viewWithTag:100];
        if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ))
        {
            checkButton.hidden = YES;
            checkButton1.hidden = NO;
        }
}

在cellforrowAtIndexPath中,我添加了两个按钮进行排序。现在,当我点击排序按钮时,该排序按钮应该消失,并且必须出现完成按钮。我给它如下,

-(void)sortItem:(id)sender
{
         NSLog(@"sort clicked");

         if(appDelegate.a = 300)
         {
               checkButton.hidden = NO;
               checkButton1.hidden = YES;
         }

          self.navigationItem.title = @"Sort Book Shelf";

          sortButton.hidden = YES;
          editButton.hidden = YES;
          doneButton.hidden = NO;

   //    if (appDelegate.b == 400) 
  //    {
 //        checkButton.hidden = NO;
 //        checkButton1.hidden = NO;
 //    }

      [editTable reloadData];
}

接下来,当我单击SORT按钮那两个复选按钮时,检查按钮1应该出现,当我单击DONE按钮时,这两个按钮应该消失。

-(void)doneClick:(id)sender
{
       NSLog(@"Done Clicked");

       if (appDelegate.b == 400) 
       {
            checkButton.hidden = YES;
            checkButton1.hidden = YES;
       }

       self.navigationItem.title = @"Edit Book Shelf";

       sortButton.hidden = NO;
       editButton.hidden = NO;
       doneButton.hidden = YES;
  }

现在,我正在使排序功能正常运行。但是当点击完成按钮时,我的按钮不会从单元格路径中消失,因为我给出条件是在第一个和最后一个索引上有按钮。如何在DONE按钮单击时隐藏我的两个按钮?非常感谢。

3 个答案:

答案 0 :(得分:0)

尝试在doneClick中调用重载数据。

[editTable reloadData];

答案 1 :(得分:0)

尝试在搜索SOF之前首先搜索谷歌,因为它可能是重复的问题

·H

NSIndexPath *selectedIndexPath;

的.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //  . . Some Code . . .
    if ( selectedIndexPath.row = indexPath.row &&  selectedIndexPath.section = indexPath.section ) {
        button1.hidden = NO;
        button2.hidden = NO;
    } else {
        button1.hidden = YES;
        button2.hidden = YES;
    }
    //  . . Some Code . . .
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    selectedIndexPath = indexPath;
    [tableView relaodData];
}

how to add button on uitableviewCell and delete previous on next cell selection

答案 2 :(得分:0)

button内提供tableView begin and end updates方法块。像

-(void)doneClick:(id)sender
{
   NSLog(@"Done Clicked");
   [tableView beginUpdates];
   if (appDelegate.b == 400) 
   {
        checkButton.hidden = YES;
        checkButton1.hidden = YES;
   }
   [tableView endUpdates];
   self.navigationItem.title = @"Edit Book Shelf";

   sortButton.hidden = NO;
   editButton.hidden = NO;
   doneButton.hidden = YES;
}