Note that there are some explanatory texts on larger screens.

plurals
  1. POtitleForHeaderSection from NSTableViewCell
    primarykey
    data
    text
    <p>How can I retrieve the current header section name from an <code>NSTableViewCell</code> item?</p> <p>Currently I have a method called <code>configureCell</code> which determines how to style the custom cell I have. This data comes from a pList file.</p> <pre><code>-(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]; } </code></pre> <p>Where I've commented out the line for <code>*country</code> I need to retrieve the current section I'm in. Currently it's statically set to <code>Finland</code> which is the array name from the pList.</p> <p>For a better understanding on how my code is laid out, here is the majority of the Table Controller.</p> <pre><code> - (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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload