在表中填充相同的数组。

时间:2015-04-08 12:59:25

标签: ios uitableview

我正在构建一个可扩展的可折叠表视图示例项目。我有四个领域。 当我点击单元格时,它会展开并显示该字段的详细信息。 我已经为字段和细节采用了5个数组。 我面临的问题是,当我点击任何单元格时,它每次都显示相同的细节。但我想展示我正在点击的字段的详细信息。 我认为问题在于我在cellForRowAtIndexPath中完成的签到。

这是我的代码

#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 allowsMultipleSelection];
}

- (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]) {

        lblDetail1.text = [self.arrPersonalInfo objectAtIndex:0];

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

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

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

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


    }
    else if ([self.arrFields objectAtIndex:indexPath.row+1]){

        lblDetail1.text = [self.arrEducation objectAtIndex:indexPath.0];

        lblDetail2.text = [self.arrEducation objectAtIndex:1];

        lblDetail3.text = [self.arrEducation objectAtIndex:2];

        lblDetail4.text = [self.arrEducation objectAtIndex:3];

        lblDetail5.text = [self.arrEducation objectAtIndex:4];

    }
    else if ([self.arrFields objectAtIndex:indexPath.row+2]){

        lblDetail1.text = [self.arrHobbies objectAtIndex:0];

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

        lblDetail3.text = [self.arrHobbies objectAtIndex:2];

        lblDetail4.text = [self.arrHobbies objectAtIndex:3];

        lblDetail5.text = [self.arrHobbies objectAtIndex:4];

    }
    else if([self.arrFields objectAtIndex:indexPath.row+3]){

        lblDetail1.text = [self.arrInterest objectAtIndex:0];

        lblDetail2.text = [self.arrInterest objectAtIndex:1];

        lblDetail3.text = [self.arrInterest objectAtIndex:2];

        lblDetail4.text = [self.arrInterest objectAtIndex:3];

        lblDetail5.text = [self.arrInterest objectAtIndex:4];

}

    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 ;
    NSLog(@"%@", 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;

    }

}

@end

3 个答案:

答案 0 :(得分:0)

KMAccordionTableViewController为您完成。

http://github.com/klevison/KMAccordionTableViewController

答案 1 :(得分:0)

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

...

if([self.arrFields objectAtIndex:indexPath.row]) {  

...

如果一直定义它,那么真的"。也许你想检查一下:

if(indexPath.row == 0) {}
else if(indexPath.row == 1) {}
...

答案 2 :(得分:0)

你应该使用部分。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if (section == 0) {
        return self.arrFields.count;
    } else if (section == 1) {
        return self.arrPersonalInfo.count;
    } else if (section == 2) {
        return self.arrEducation.count;
    } else if (section == 3) {
        return self.arrHobbies.count;
    } else if (section == 4) {
        return self.arrInterest.count;
    } else {
        return 0;
    }
}

如果你有一个完整的tableView与部分等工作,然后处理折叠/扩展:

  • 如果折叠则设置空数组,如果展开则设置正确的数组
  • 使用self.tableView reloadSections:withRowAnimation