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

标签 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 对象并点击后它应该再次崩溃。profileArray 也是如此。 AboutSteponeArrayprofileArraysectionTitleArray 的部分。

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

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

viewLabel.text=[sectionTitleArray objectAtIndex:section];

由于您的 sectionTitleArray 的每个元素都是一个 NSDictionnary,因此您要将 NSDictionnay 分配给标签 tetxt 属性,该属性是 NSString 输入。

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

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

关于ios - 在 iOS 中使用 NSDictionary 的可扩展 UITableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671185/

相关文章:

ios - 授予地址簿访问权限后,将用户联系人存储在 NSMutable 数组中

ios - 如何正确裁剪个人资料照片?

ios - 记录中特定字段的 cloudkit 通知

objective-c - UnsafeMutablePointer 从 Swift 调用函数

iphone - NSURL 连接在 3g 上失败 'network connection was lost'

objective-c - 获取分组 UITableView 中单元格的宽度以设置节标题宽度

ios - 为设置创建一个 UITableView。

ios - 在 UIPageViewController 中设置 UICollectionView 的内容插入?

SQLite 表不存在

ios - 适用于 iPhone 的真实表格组件