如何从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];
    }

最佳答案

看来您已经有了[[self.milkTypes allKeys] objectAtIndex:section]-只需使用它即可,因此

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

关于ios - 来自NSTableViewCell的titleForHeaderSection,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8444923/

10-11 17:12