gpt4 book ai didi

ios - 来自 NSTableViewCell 的 titleForHeaderSection

转载 作者:行者123 更新时间:2023-11-28 23:10:25 26 4
gpt4 key购买 nike

如何从 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 的名称。

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

    - (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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com