ios - 如何在 UITableView 中选择表头?

标签 ios uitableview

我需要一个 tableView,我可以在其中选择一个部分来向其中添加一个项目。如何?我是否应该将高度从 22px 增加到 44px 以保持背景样式并在 sectionnview 上实现事件处理程序?您使用什么方法在部分中添加项目?

最佳答案

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    foundIndex=FALSE;
    for (int i=0; i<[indexArray count]; i++)
    {
        _currentRow=[[indexArray objectAtIndex:i] intValue];
        if(_currentRow==indexPath.section)
        {
            foundIndex=TRUE;
        }
    }
    if (foundIndex==TRUE)
    {
        return 100;
    }
    else
    {
        return 0;
    }   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
UIButton *btnCollapse = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnCollapse setFrame:CGRectMake(10, 0, 303, 50)];
    [btnCollapse setBackgroundColor:[UIColor clearColor]];
    [btnCollapse addTarget:self action:@selector(touchedSection:) forControlEvents:UIControlEventTouchUpInside];
    btnCollapse.tag = section+600;
    [headerView addSubview:btnCollapse];

    UILabel *lot_title = [[UILabel alloc]initWithFrame:CGRectMake(13, 2, 270, 20)];
    lot_title.numberOfLines=3;
    lot_title.backgroundColor=[UIColor clearColor];
    lot_title.text=[NSString stringWithFormat:@"%@",[[MyConsignmentData objectAtIndex:section] objectForKey:@"lot_title"]];
    lot_title.font = [UIFont boldSystemFontOfSize:14];
    lot_title.textColor =[UIColor blackColor];

    CGSize maxSize = CGSizeMake(lot_title.bounds.size.width, CGFLOAT_MAX);
    CGSize textSize1 = [lot_title.text sizeWithFont:lot_title.font constrainedToSize:maxSize];
    lot_title.frame=CGRectMake(13, 2, 270, textSize1.height);

    if (textSize1.height+10<40)
    {
        return 40;
    }
    else
    {
        return textSize1.height+10;
    }
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)];

    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(280,tableView.sectionHeaderHeight/2+5 , 22, 22)];

    foundIndex=FALSE;
    for (int i=0; i<[indexArray count]; i++)
    {
        _currentRow=[[indexArray objectAtIndex:i] intValue];
        if(_currentRow==section)
        {
            foundIndex=TRUE;
        }
    }
    if (foundIndex==TRUE)
    {
        img.image = [UIImage imageNamed:@"down-arrow-active.png"];
    }
    else
    {
        img.image = [UIImage imageNamed:@"right-arrow-deactive.png"];
    }

    [headerView addSubview:img];

    UIButton *btnCollapse = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnCollapse setFrame:CGRectMake(10, 0, 303, 50)];
    [btnCollapse setBackgroundColor:[UIColor clearColor]];
    [btnCollapse addTarget:self action:@selector(touchedSection:) forControlEvents:UIControlEventTouchUpInside];
    btnCollapse.tag = section+600;
    [headerView addSubview:btnCollapse];

     UIImageView *imgbac;
    if (textSize1.height+10<40)
    {
        imgbac = [[UIImageView alloc]initWithFrame:CGRectMake(8, 0, 303, 40)];
    }
    else
    {
        imgbac = [[UIImageView alloc]initWithFrame:CGRectMake(8, 0, 303, textSize1.height+10)];
    }

    imgbac.image = [UIImage imageNamed:@"mybid-box-bg.png"];
    [headerView addSubview:imgbac];

    [headerView addSubview:imgbac];
    [headerView addSubview:lot_title];
    [headerView addSubview:img];
    [headerView addSubview:btnCollapse];
    headerView.backgroundColor=[UIColor clearColor];
    return headerView;
}
- (IBAction)touchedSection:(id)sender
{
    NSInteger _index=[sender tag]-600;
    foundIndex=FALSE;
    NSInteger stored;
    for (int i=0; i<[indexArray count]; i++)
    {
        _currentRow=[[indexArray objectAtIndex:i] intValue];
        if(_currentRow==_index)
        {
            stored=i;
            foundIndex=TRUE;
        }
    }
    if (foundIndex==TRUE)
    {
        [indexArray removeObjectAtIndex:stored];
    }
    else
    {
        [indexArray addObject:[NSNumber numberWithUnsignedInteger:_index]];
    }

    [tableview reloadData];
}

关于ios - 如何在 UITableView 中选择表头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26500396/

相关文章:

iphone - 在没有导航 Controller 堆栈的情况下,以动画方式将 View Controller 从一个 View Controller 过渡到另一个 View Controller

ios - 通话中状态栏将 UIPageViewController 内容 View 向下推

ios - GLKView.display() 方法有时会导致崩溃。 EXC_BAD_ACCESS

ios - UITableViewCell 中的 UIButton 在 reloadRowsAtIndexPath 上跳转

ios - 如何在编辑模式下(在 UITableView 中)执行不同的 segue?

ios - 带有 UINavigationController 作为选项卡的 UITabViewController

ios - 在框架内使用 xcframework

objective-c - 在第一个 uitableview 部分上方添加空间

ios - 重绘时未清除 UIBezierPath

ios - 如何从 UITableView 单元格中获取 id 完整数组值?