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

时间:2016-01-08 06:58:26

标签: ios objective-c 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个对象,然后再次点击它会崩溃。{{{{{ 1}}。 profileArrayAboutSteponeArrayprofileArray

的各个部分

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

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSDictionaryI rangeOfCharacterFromSet:]:无法识别的选择器发送到实例0x7f9cfb4bf880'错误。

2 个答案:

答案 0 :(得分:2)

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

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)section的以下行:

viewLabel.text=[sectionTitleArray objectAtIndex:section];

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

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

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

答案 1 :(得分:0)

我检查了你的代码&amp;发现了一些问题。这是更正后的代码:

首先从数组中删除了键值:

sectionTitleArray = [NSArray arrayWithObjects:@"About Step0ne", @"Profile", @"Matches", @"Messages", @"Photos", @"Settings", @"Privacy", @"Reporting issues", nil];
aboutStep0neArray = [NSArray arrayWithObjects:@"stepone1", @"stepone2", @"stepone3", @"stepone4", @"stepone5", @"stepone6", @"stepone7", @"stepone8", nil];
profileArray = [NSArray arrayWithObjects:@"profile1", @"profile2", @"profile3", @"profile4", @"profile5", @"profile6", @"profile7", @"profile8", nil];

<强>原因: 您已经为所有对象提供了通用键,而不是为什么需要提供键。那就是创造了一个被删除的问题。

<强>比: 在Tableview方法中:

- (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];
        } else if (indexPath.section == 1) {
            cell.textLabel.text = [profileArray objectAtIndex:indexPath.row];
        }
    }
    return cell;
}

这是我做过的唯一改变&amp;测试。它对我来说很好。

希望它对你有用。