简体   繁体   中英

iPhone sdk how to add/remove UIButton on two different button clicks

I have two separate buttons for sorting an array in ascending and descending order respectively in 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;
        }
}

In cellforrowAtIndexPath i added two buttons for sorting. And now when i click on sort button, that sort button should be disappear and done button must appear. And i gave it as follows,

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

Next when i click SORT button those two check button, check button1 should appear and when i click DONE button those two buttons should disappear.

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

Now,am getting sort functionality working well. But when am tapping done button my buttons are not disappearing from cell path, since i gave the condition to have the buttons on first and last index. How can i hide my two buttons only on DONE button click? Thanks a lot in advance.

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

[editTable reloadData];

try to search on google first before asking on SOF as it may be duplicate question

.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

Give your button method block, inside tableView begin and end updates . Like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM