首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自NSTableViewCell的titleForHeaderSection

来自NSTableViewCell的titleForHeaderSection
EN

Stack Overflow用户
提问于 2011-12-09 19:37:25
回答 1查看 312关注 0票数 0

如何从NSTableViewCell项目中检索当前标题部分名称?

目前,我有一个名为configureCell的方法,用于确定如何设置自定义单元格的样式。此数据来自pList文件。

代码语言:javascript
复制
-(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中的数组名称。

为了更好地理解我的代码是如何布局的,这里是表控制器的主要部分。

代码语言:javascript
复制
    - (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];
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-09 21:22:43

似乎你已经有了它,在[self.milkTypes allKeys对象already :节]中-只需使用它来代替,所以

代码语言:javascript
复制
NSString *country = [[self.milkTypes allKeys] objectAtIndex: indexPath.section];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8444923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档