UITableView с несколькими разделами и аксессуарами

Это мои функции для моего представления таблицы.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return [self.categories count];

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {


    return @"test";
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%d", self.collections.count);
    return [self.collections count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    switch ([indexPath section])
    {
        case 0:
            self.myList = self.collections;
            break;
    }

        cell.textLabel.text = self.collections[indexPath.row];

    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 48;
}

На самом деле, с этим кодом в моем табличном представлении все разделы и ячейки отображаются в одном и том же представлении.

Однако мне бы хотелось, чтобы табличное представление отображало в первый раз строку с названием моего раздела. Когда я нажимаю на строку, где находится мой заголовок раздела, я хотел бы показать ячейки, которые находятся в разделе. Как я могу это сделать? Мне нужно 2 tableView?


person Malicia    schedule 28.05.2014    source источник
comment
Итак, вы хотите сделать сворачиваемую таблицу? ну... в таком случае Нет вам не нужно 2 tableViews. Вы можете сделать это, просто приложив немного усилий, но обратите внимание, что это полностью зависит от источника данных. Как вы структурировали свой источник данных? Я имею в виду, это массив словарей или что?   -  person staticVoidMan    schedule 28.05.2014
comment
посмотрите на эту ссылку: Expandable/Collapsable Accordion UITableView, чтобы получить общее представление о во что вы будете ввязываться   -  person staticVoidMan    schedule 28.05.2014
comment
Мои источники данных находятся в NSmutableArray.   -  person Malicia    schedule 28.05.2014
comment
Я не хочу аккордеон. Когда я нажимаю на заголовок раздела, мне нужно новое представление таблицы только с моими элементами.   -  person Malicia    schedule 28.05.2014
comment
хорошо, так что и в этом случае вы все еще можете обойтись одним tableView, соответствующим образом изменив источник данных. Но сначала... что находится в self.categories и self.collections? и... Как вы собираетесь заполнять заголовок раздела (я полагаю, из self.categories)? Пожалуйста, включите достаточное количество следующих результатов: [1] NSLog(@"%@",self.categories) и [2] NSLog(@"%@",self.collections), чтобы получить более четкое представление о том, что у вас есть.   -  person staticVoidMan    schedule 28.05.2014


Ответы (1)


Вам нужно будет взять два uitableview, в этом делегате будет то же самое, после чего вам придется использовать этот код

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%@",indexPath];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell==Nil)
    {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    if (isDeptSelected)
    {
        //[cell.imageView setImage:[UIImage imageNamed:@"Department.png"]];
        cell.textLabel.text = [[arrDept_Detail valueForKey:@"dep_Name"]objectAtIndex:indexPath.row];
    }
    else if (isEmpSelected)
    {
        [cell.imageView setImage:[UIImage imageNamed:@"Emp_Images"]];
        cell.textLabel.text = [[arrEmp_Detail valueForKey:@"emp_Name"]objectAtIndex:indexPath.row];

    }


   // cell.textLabel.text=[[arrDept_Detail objectAtIndex:indexPath.row]valueForKeyPath:@"dep_Name"];
    [cell setBackgroundColor:[UIColor brownColor]];
    [cell.textLabel setTextColor:[UIColor greenColor]];
    [cell.textLabel setFont:[UIFont fontWithName:@"Georgia-Bold" size:16]];
     cell.textLabel.highlightedTextColor=[UIColor purpleColor];
    [self.view setBackgroundColor:[UIColor brownColor]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (isDeptSelected)
    {
        isDeptSelected=false;
        isEmpSelected=true;
        [btnDoneclick setHidden:NO];
        [self.view addSubview:btnDoneclick];
        EmpInDepTableView=[[UITableView alloc]initWithFrame:CGRectMake(160, 284,0, 0)];
        [EmpInDepTableView setDelegate:self];
        [EmpInDepTableView setDataSource:self];
        [EmpInDepTableView setHidden:NO];
        [EmpInDepTableView setBackgroundColor:[UIColor brownColor]];
        EmpInDepTableView.layer.borderWidth=3.0f;
        EmpInDepTableView.layer.cornerRadius=10.0f;
        [self.view addSubview:EmpInDepTableView];
        self.tabBarController.tabBar.userInteractionEnabled=NO;
        UITableViewCell *cell=[Dept_TableView cellForRowAtIndexPath:indexPath];
        DeptId=[Dep_Detail fetchDeptId_DeptName:cell.textLabel.text];
        arrEmp_Detail = [[Emp_Detail fetchEmp_By_DeptId:DeptId]mutableCopy];

        [UITableView animateWithDuration:0.6 animations:^
         {
             [EmpInDepTableView setFrame:CGRectMake(0, 64,320, 430)];
             [btnDoneclick setFrame:CGRectMake(0,490,320,29)];
         }completion:^(BOOL finished)
         {
             [EmpInDepTableView reloadData];

         }];

    }
    else if (isEmpSelected)
    {

    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] init];
    [view setBackgroundColor:[UIColor brownColor]];
    return view;
}

-(IBAction)btnDoneclicked:(id)sender
{
    [EmpInDepTableView reloadData];

    isDeptSelected=true;
    isEmpSelected=false;
    [btnDoneclick setHidden:YES];
    self.tabBarController.tabBar.userInteractionEnabled=YES;

    [UITableView animateWithDuration:0.6 animations:^
    {
        [EmpInDepTableView setFrame:CGRectMake(160, 284, 0, 0)];
        [btnDoneclick setFrame:CGRectMake(160, 284, 0, 0)];
        [self.view setBackgroundColor:[UIColor brownColor]];



    }completion:^(BOOL finished)
    {

    }];


}
person IOS_DEVELOPER    schedule 28.05.2014