ios - 第二次点击单元格时如何折叠展开的单元格?

标签 ios objective-c uitableview

我已经构建了一个示例可扩展 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;

    }

}

最佳答案

试试这个

@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;

     }

 }

关于ios - 第二次点击单元格时如何折叠展开的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531002/

相关文章:

android - 如何扩展我的 Cordova/PhoneGap 应用程序?我应该使用 "cordova-anyscreen"还是太老套了?

ios - textFieldShouldReturn 和 shouldChangeCharactersInRange

ios - XCode:UIWebView 邮件链接不工作?

objective-c - 多个图像/按钮相对于可缩放图像上的位置保持不变

iphone - 设置分组 UITableViewCell 的背景颜色

ios - 自定义单元格内容不显示 Swift

ios - 搜索对象数组以返回与属性值匹配的对象

iPhone 如何将大图像与 JSON 文件一起发送?我应该将它嵌入到 JSON 中还是单独发送?

ios - UITableView 中的 UISearchBar

ios - SDWebImage无法从NSDictionary加载图像