2011-12-09 52 views
0

如何從NSTableViewCell項目中檢索當前標題部分名稱?titleForHeaderSection from NSTableViewCell

目前我有一種方法稱爲configureCell,它確定如何樣式自定義單元格。這些數據來自pList文件。

-(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath{ 
    UILabel *label; 
    UIView *backView; 

    // NSString *country = [self tableView: tableViewCell titleForHeaderInSection:indexPath.section]; 
    NSString *fatPercent = [[self.milkTypes valueForKey:@"Finland"] objectAtIndex:indexPath.row]; 

    label = (UILabel *)[tableViewCell viewWithTag:1]; 
    label.text = @"Fat Percent test"; 

    backView = (UIView *)[tableViewCell viewWithTag:2]; 

    backView.backgroundColor = [self colorWithHexString: fatPercent]; 
} 

在哪裏我註釋掉線*country我需要檢索當前章節我在。目前,它是靜態設置爲Finland這是從plist中數組名。

爲了更好地理解我的代碼是如何佈置的,這裏是大部分表控制器。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return [self.milkTypes count]; 
    } 

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
      return [[self.milkTypes allKeys] objectAtIndex:section]; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
      NSString *country = [self tableView:tableView titleForHeaderInSection:section]; 
      return [[self.milkTypes valueForKey:country] count]; 
    } 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
     NSString *cellIdentifier; 
     UITableViewCell *cell; 
     cellIdentifier = [NSString stringWithFormat:@"MilkCell"]; 

     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (cell==nil) { 
      cell = [self tableViewCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 
     } 
     [self configureCell:cell forIndexPath:indexPath]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     return cell; 
    } 


    -(UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{ 
     CGRect rect; 
     UILabel *label; 
     UIView *backView; 
     UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 

     float center = (float) (40-20)/2; 

     rect = CGRectMake(15, center, 270, 20); 
     label = [[UILabel alloc] initWithFrame:rect]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; 
     label.tag = 1; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textColor = [UIColor blackColor]; 
     [cell.contentView addSubview:label]; 
     [label release]; 

     rect = CGRectMake(280, 10, 20, 40-20); 
     backView = [[UIView alloc] initWithFrame:rect]; 
     backView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth; 
     backView.backgroundColor = [UIColor clearColor]; 
     backView.tag = 2; 

     [cell.contentView addSubview:backView]; 
     [backView release]; 

     return cell; 
    } 

    -(void)configureCell:(UITableViewCell *)tableViewCell forIndexPath:(NSIndexPath *)indexPath { 
     UILabel *label; 
     UIView *backView; 

    //  NSString *country = [self tableView: tableViewCell titleForHeaderInSection:indexPath.section]; 
      NSString *fatPercent = [[self.milkTypes valueForKey:@"Sweden"] objectAtIndex:indexPath.row]; 

     label = (UILabel *)[tableViewCell viewWithTag:1]; 
     label.text = @"Fat Percent test"; 

     backView = (UIView *)[tableViewCell viewWithTag:2]; 

      backView.backgroundColor = [self colorWithHexString: fatPercent]; 
    } 
+0

你沒有節頭的數組反正您可以使用indexPath.section值進行選擇? – reddersky

+0

愚蠢的沒有。我對iOS開發很有新意,並試圖讓我的頭腦發現我習慣的一些差異。在數組中存儲段的示例? – James

+0

即使我把它放在一個數組中,我將如何從它得到正確的部分? – James

回答

2

看來你已經擁有它,無論如何,在[self.milkTypes allKeys] objectAtIndex:§ - 只需要使用代替,所以

NSString *country = [[self.milkTypes allKeys] objectAtIndex: indexPath.section];