2014-10-22 63 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]; 
} 
相關問題