2014-10-22 30 views
0

我需要一个tableView,我可以选择一个部分添加一个项目。怎么样?我应该将高度从22px增加到44px保留背景样式并在sectionnview上实现事件处理程序?你有什么aproach你用来添加项目的一节?如何在UITableView中选择标题?

回答

1

有没有办法与UITableViewDelegate做到这一点。

创建自定义部分headerView,然后向其添加“点按”手势。

LIKE:

UITapGestureRecognizer *singleTapRecogniser = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)] autorelease]; 
[singleTapRecogniser setDelegate:self]; 
singleTapRecogniser.numberOfTouchesRequired = 1; 
singleTapRecogniser.numberOfTapsRequired = 1; 
[yourHeaderView addGestureRecognizer:singleTapRecogniser]; 

然后:

//This method will be called on Tap of section heade 
- (void) handleGesture:(UIGestureRecognizer *)gestureRecognizer{ 
} 
1
- (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]; 
}