UITableView有多个section和accessoryType



这是我的表视图的函数。

- (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;
}

实际上,通过这段代码,我的表视图在同一个视图中显示了所有的section和单元格。

但是,我想要一个表视图,它第一次显示我的标题为section的行。当我点击行,那里有我的节的标题,我想显示单元格是在节。我怎么能做到呢?我需要2个tableview吗?

你应该在那个委托中使用两个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)
    {
    }];

}

最新更新