【问题标题】:UITableView with multiple sections and accessoryType具有多个部分和附件类型的 UITableView
【发布时间】:2014-05-28 14:50:39
【问题描述】:

这是我的表格视图的功能。

- (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 个 tableViews 吗?

【问题讨论】:

  • 所以你想制作一个可折叠的表格视图?好吧...在那种情况下,你不需要需要 2 tableViews。您可以只需一点点努力就可以做到这一点,但请注意它完全依赖于数据源。您是如何构建数据源的?我的意思是它是字典数组还是什么?
  • 查看此链接:Expandable/Collapsable Accordion UITableView 大致了解您将进入的内容
  • 我的数据源在 NSMutableArray 中。
  • 我不想要手风琴。当我点击部分的标题时,我想要一个只有我的项目的新表格视图。
  • 好的,所以在这种情况下,您仍然可以通过适当地更改数据源来使用单个 tableView。但首先...self.categoriesself.collections 中有什么?并且...您打算如何填充节标题(我假设来自self.categories)?请包含足够多的以下结果:[1] NSLog(@"%@",self.categories)[2] NSLog(@"%@",self.collections) 以便更清楚地了解您拥有的内容

标签: ios uitableview cell sections


【解决方案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)
    {

    }];


}

【讨论】:

    猜你喜欢
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多