簡體   English   中英

第二次在單元格上點擊時,展開的單元格如何崩潰?

[英]How collapse the expanded cell when tapped for the second time on the cell?

我已經建立了一個示例可擴展tableview項目。 一切正常。 現在,當我第二次點擊同一單元格時,我想最小化展開的單元格。 誰能告訴我我該怎么做? 非常感謝您的幫助。

這是我的代碼

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    NSMutableArray *arrSelected;
    NSIndexPath *selectedRowIndex;
    BOOL *isTwoTaps;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.arrFields = @[@"Personal Info", @"Education", @"Hobbies", @"Interest"];
    self.arrPersonalInfo = @[@"First Name", @"Last Name", @"Age", @"Phone Number",@"Address"];
    self.arrEducation = @[@"10", @"10+2", @"Graduation", @"Masters", @"Higher Studies"];
    self.arrHobbies = @[@"Sports", @"Cooking", @"Music", @"Gardening", @"Fishing"];
    self.arrInterest = @[@"Collection", @"Space", @"Medical", @"Engineering", @"Programming"];
    [self.tableView setAllowsMultipleSelection:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arrFields count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier=@"CellA";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    UIView *vw=(UIView *)[cell.contentView viewWithTag:999];
    UILabel *lblField = (UILabel *)[cell.contentView viewWithTag:1];
    lblField.text = [self.arrFields objectAtIndex:indexPath.row];

    UILabel *lblDetail1 = (UILabel *)[cell.contentView viewWithTag:2];
    UILabel *lblDetail2 = (UILabel *)[cell.contentView viewWithTag:3];
    UILabel *lblDetail3 = (UILabel *)[cell.contentView viewWithTag:4];
    UILabel *lblDetail4 = (UILabel *)[cell.contentView viewWithTag:5];
    UILabel *lblDetail5 = (UILabel *)[cell.contentView viewWithTag:6];


    if([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Personal Info"]) {

        lblDetail1.text = [self.arrPersonalInfo objectAtIndex:indexPath.row];

        lblDetail2.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+1];

        lblDetail3.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+2];

        lblDetail4.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+3];

        lblDetail5.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+4];

        NSLog(@"%@", [self.arrFields objectAtIndex:indexPath.row]);


    }
    else if ([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Education"]){

        lblDetail1.text = [self.arrEducation objectAtIndex:indexPath.row-1];

        lblDetail2.text = [self.arrEducation objectAtIndex:indexPath.row];

        lblDetail3.text = [self.arrEducation objectAtIndex:indexPath.row+1];

        lblDetail4.text = [self.arrEducation objectAtIndex:indexPath.row+2];

        lblDetail5.text = [self.arrEducation objectAtIndex:indexPath.row+3];

    }
    else if ([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Hobbies"]){

        lblDetail1.text = [self.arrHobbies objectAtIndex:indexPath.row-2];

        lblDetail2.text = [self.arrHobbies objectAtIndex:indexPath.row-1];

        lblDetail3.text = [self.arrHobbies objectAtIndex:indexPath.row];

        lblDetail4.text = [self.arrHobbies objectAtIndex:indexPath.row+1];

        lblDetail5.text = [self.arrHobbies objectAtIndex:indexPath.row+2];

   }
    else if([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Interest"]){

        lblDetail1.text = [self.arrInterest objectAtIndex:indexPath.row-3];

       lblDetail2.text = [self.arrInterest objectAtIndex:indexPath.row-2];

       lblDetail3.text = [self.arrInterest objectAtIndex:indexPath.row-1];

       lblDetail4.text = [self.arrInterest objectAtIndex:indexPath.row];

        lblDetail5.text = [self.arrInterest objectAtIndex:indexPath.row+1];

}

   if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {

         vw.hidden=FALSE;
    }
    else
   {
      vw.hidden=TRUE;
    }
    return cell;

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

  if(selectedRowIndex)
   {
       [tableView reloadData];
   }


    selectedRowIndex = indexPath ;

    [tableView beginUpdates];

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [tableView endUpdates];

}

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {

        return 222;

    }
    else{

        return 44;

    }

}

接受一個像isCellExpanded這樣的布爾變量,使該值在首次單擊時為true,在用戶再次單擊該值時使其為false。

嘗試這個

@implementation ViewController
{
    NSMutableArray *arrSelected;
    NSIndexPath *selectedRowIndex;
    BOOL *isTwoTaps;
    NSInteger selectedIndex;

}

 - (void)viewDidLoad {
     [super viewDidLoad];

    selectedIndex=-1;
  }

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

  if(selectedRowIndex)
  {
       [tableView reloadData];
  }


  selectedRowIndex = indexPath ;
  if (selectedIndex==indexPath.row)
  {
      selectedIndex=-1;
  }
  else
 {
      selectedIndex=indexPath.row;
 }

[tableView beginUpdates];

[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableView endUpdates];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

     if(selectedIndex==indexPath.row) {

        return 222;

    }
    else{

        return 44;

     }

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM