2011-10-29 11 views
0

テーブルの一番上に表示される別のセクションをテーブルビューに追加する必要があります。私はまた、私はコンテンツをうまくフォーマットすることができますカスタムビューを使用したいと思います。UITableViewにセクションタイトルとセクションタイトルを1回だけ追加するには?

下の図では、セクションヘッダーとその下の行があります。

私は何度も試しましたが、必要なセクションの数と、必要なすべての行を表示するロジックについてちょっと混乱しています。問題をさらに複雑にするために、1つのセクションに1つの行が表示されます。テキストは利用可能な行がないことを示しています。この場合、私は新しいセクションとそのテキストを表示したいと思います。

enter image description here

ここに私のコードだ...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    int count = [sectionTitles count]; 
    if (count == 0) { 
     return 1; 
    } else { 
     return count; 
    } 
} 

//

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: 
      (NSInteger)section { 
    int count = 0; 
    if (sectionRowCounts.count != 0) { 
     NSString *title = [sectionTitles objectAtIndex:section]; 
     count = [[sectionRowCounts objectForKey:title] intValue]; 
    } 
    return (count ? count : 1); 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: 
    (NSInteger)section { 
    NSString *title = @""; 
    if (sectionTitles.count != 0) { 
     title = [sectionTitles objectAtIndex:section]; 
    } 
    return title; 
} 

//

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection: 
    (NSInteger)section { 
    if (sectionTitles.count == 0) { 
     UIView *customView = [[[UIView alloc] init] autorelease]; 
     return customView; 
    } 

    HeaderSectionReportView *customView = [[[HeaderSectionReportView alloc] 
     initWithFrame:CGRectMake(0.0, 0.0, 360.0, 20.0)] autorelease]; 

    // create the button object 
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    headerLabel.frame = CGRectMake(10.0, 0.0, 100.0, 20.0); 
    headerLabel.text = [sectionTitles objectAtIndex:section]; 
    [customView addSubview:headerLabel]; 
    [headerLabel release]; 

    return customView; 
} 

- (CGFloat) tableView:(UITableView *)tableView 
     heightForHeaderInSection:(NSInteger)section { 
    return 20.0; 
} 

//

あなたは確かにそれに関連付けられたカスタムビューの先頭に別のセクションを追加することもできますが
- (UITableViewCell *)tableView:(UITableView *)tableView 
      cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell1; 
    if(rowSections.count == 0) { 

     cell1 = [[[UITableViewCell alloc] initWithStyle: 
      UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; 

     UIView *view = [[[UIView alloc] init] autorelease]; 
     view.backgroundColor = [UIColor clearColor]; 
     cell1.selectedBackgroundView = view; 

     cell1.textLabel.text = @"No transactons found"; 
     return cell1; 
    } 

    NSString *strRowSection = [NSString stringWithFormat:@"%i-%i", 
     indexPath.section, indexPath.row]; 
    NSInteger intDBRow = [rowSections indexOfObject:strRowSection]; 
    static NSString *CellIdentifier = @"ReportCell"; 

    ReportCell *cell = (ReportCell *) [tableView 
      dequeueReusableCellWithIdentifier:CellIdentifier]; 

//

if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] 
     loadNibNamed:@"ReportCell" owner:nil options:nil]; 

     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell = ((ReportCell *) currentObject); 
       break; 
      } 
     } 
    } 

    NSString *desc = [[transactionsArray objectAtIndex:intDBRow] 
      objectForKey:@"desc"]; 
    cell.descriptionValue.text = desc; 

    UIView *view = [[[UIView alloc] init] autorelease]; 
    view.backgroundColor = [UIColor clearColor]; 
    cell.selectedBackgroundView = view; 

    return cell; 

} 

//

- (void) populateDataSource { 

    int sectionIncr = -1; 
    int rowIncr = 0; 

    sectionTitles = [[NSMutableArray alloc] initWithObjects:nil]; 
    sectionRowCounts = [[NSMutableDictionary alloc] init]; 
    rowSections = [[NSMutableArray alloc] initWithObjects:nil]; 

    NSMutableArray *arrayTmp= [[NSMutableArray alloc] init]; 

    //populate array 

    self.transactionsArray = arrayTmp; 
    [arrayTmp release]; 

} 

答えて

2

は別のオプションがあります。 UITableViewプロパティ 'tableHeaderView'にカスタムビューを割り当てることができます。アップルのドキュメントのtableHeaderViewを参照してください。

関連する問題