我需要一个 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/