在iOS中使用NSDictionary的可扩展UITableview单元格

这是我的代码:

Viewcontroller.m

@interface ViewController ()
{
    NSArray *sectionTitleArray;
    NSMutableArray *arrayForBool;

    NSArray *aboutStep0neArray;
    NSArray *profileArray;
}

@property (nonatomic, strong) IBOutlet UITableView *tablevw;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tablevw.delegate = self;
    _tablevw.dataSource = self;

    arrayForBool = [[NSMutableArray alloc]init];

    sectionTitleArray = @[ @{@"text": @"About Step0ne"},
                           @{@"text": @"Profile"},
                           @{@"text": @"Matches"},
                           @{@"text": @"Messages"},
                           @{@"text": @"Photos"},
                           @{@"text": @"Settings"},
                           @{@"text": @"Privacy"},
                           @{@"text": @"Reporting issues"},
                          ];

    aboutStep0neArray = @[ @{@"text1": @"stepone1"},
                           @{@"text1": @"stepone2"},
                           @{@"text1": @"stepone3"},
                           @{@"text1": @"stepone4"},
                           @{@"text1": @"stepone5"},
                           @{@"text1": @"stepone6"},
                           @{@"text1": @"stepone7"},
                           @{@"text1": @"stepone8"},
                         ];

    profileArray = @[ @{@"text2": @"profile1"},
                      @{@"text2": @"profile2"},
                      @{@"text2": @"profile3"},
                      @{@"text2": @"profile4"},
                      @{@"text2": @"profile5"},
                      @{@"text2": @"profile6"},
                      @{@"text2": @"profile7"},
                      @{@"text2": @"profile8"},
                    ];



    for (int i=0; i<[sectionTitleArray count]; i++)
    {
        [arrayForBool addObject:[NSNumber numberWithBool:NO]];

    }

}

#pragma mark:- UITableView Delegate and Datasource Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return sectionTitleArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([[arrayForBool objectAtIndex:section] boolValue])
    {
        if (section == 0)
        {
            return [aboutStep0neArray count];
        }
        else if (section == 1)
        {
            return [profileArray count];
        }
     }
     return 1;
 }  

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
  reuseIdentifier:cellIdentifier];
    }
    BOOL manyCells = [[arrayForBool objectAtIndex:indexPath.section]boolValue];

    if (!manyCells)
    {
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.text = @"";
    }
    else
    {
        if (indexPath.section == 0)
        {
            cell.textLabel.text = [[aboutStep0neArray objectAtIndex:indexPath.row]objectForKey:@"text1"];
        }
        else if (indexPath.section == 1)
        {
            cell.textLabel.text = [[profileArray objectAtIndex:indexPath.row]objectForKey:@"text2"];
        } 
    }
    return cell;
}


 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
/*************** Close the section, once the data is selected ***********************************/
    [arrayForBool replaceObjectAtIndex:indexPath.section withObject:[NSNumber
  numberWithBool:NO]];

    [_tablevw reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]
  withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[arrayForBool objectAtIndex:indexPath.section] boolValue])
    {
        return 40;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

#pragma mark - Creating View for TableView Section

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 280,40)];
    sectionView.tag=section;
    UILabel *viewLabel=[[UILabel alloc]initWithFrame:CGRectMake(13, 0, _tablevw.frame.size.width-10,
  40)];
    viewLabel.backgroundColor=[UIColor clearColor];
    viewLabel.textColor=[UIColor purpleColor];
    viewLabel.font=[UIFont systemFontOfSize:15];
    viewLabel.text=[sectionTitleArray objectAtIndex:section];
    [sectionView addSubview:viewLabel];

    /********** Add a custom Separator with Section view *******************/
    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(12, 40, _tablevw.frame.size.width-35, 1)];
    separatorLineView.backgroundColor = [UIColor purpleColor];
    [sectionView addSubview:separatorLineView];

    /********** Add UITapGestureRecognizer to SectionView   **************/

    UITapGestureRecognizer  *headerTapped   = [[UITapGestureRecognizer alloc] initWithTarget:self
  action:@selector(sectionHeaderTapped:)];

    [sectionView addGestureRecognizer:headerTapped];

    return  sectionView; 
 }

#pragma mark - Table header gesture tapped

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];

    if (indexPath.row == 0)
    {
        BOOL collapsed  = [[arrayForBool objectAtIndex:indexPath.section] boolValue];
        for (int i=0; i<[sectionTitleArray count]; i++)
        {
            if (indexPath.section==i)
            {
                [arrayForBool replaceObjectAtIndex:i withObject:[NSNumber numberWithBool:!collapsed]];
            }
        }
        [_tablevw reloadSections:[NSIndexSet indexSetWithIndex:gestureRecognizer.view.tag]
  withRowAnimation:UITableViewRowAnimationAutomatic];

     }

     [_tablevw reloadData];
}

首先在tableview sectionTitleArray对象中显示.之后当我单击About Stepone行时,它应该使用aboutSteponeArray对象进行扩展,再次单击后它应该会崩溃.同样也可以使用profileArray. AboutSteponeArray和profileArray是sectionTitleArray的部分.

当我运行我的代码时,我得到了:

Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSDictionaryI rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0x7f9cfb4bf880’ error.

最佳答案 您的数组不正确地实例化:不要在每个数组中的最后一个元素之后添加逗号

sectionTitleArray = @[ @{@"text": @"About Step0ne"},
                       @{@"text": @"Profile"},
                       @{@"text": @"Matches"},
                       @{@"text": @"Messages"},
                       @{@"text": @"Photos"},
                       @{@"text": @"Settings"},
                       @{@"text": @"Privacy"},
                       @{@"text": @"Reporting issues"}
                     ];

aboutStep0neArray = @[ @{@"text1": @"stepone1"},
                       @{@"text1": @"stepone2"},
                       @{@"text1": @"stepone3"},
                       @{@"text1": @"stepone4"},
                       @{@"text1": @"stepone5"},
                       @{@"text1": @"stepone6"},
                       @{@"text1": @"stepone7"},
                       @{@"text1": @"stepone8"}
                     ];

profileArray = @[ @{@"text2": @"profile1"},
                  @{@"text2": @"profile2"},
                  @{@"text2": @"profile3"},
                  @{@"text2": @"profile4"},
                  @{@"text2": @"profile5"},
                  @{@"text2": @"profile6"},
                  @{@"text2": @"profile7"},
                  @{@"text2": @"profile8"}
                ];

编辑:

您的问题的根源在 – (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection 🙁 NSInteger)部分位于以下行:

viewLabel.text=[sectionTitleArray objectAtIndex:section];

由于sectionTitleArray的每个元素都是NSDictionnary,因此您将NSDictionnay分配给标签为tetS的属性,该属性是NSString类型.

由于存储在sectionTitleArray中的所有词典中的所有键都是@“text”,因此请替换为以下内容:

viewLabel.text= [[sectionTitleArray objectAtIndex:section] objectForKey:@"text"];
点赞