如何从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];
}发布于 2011-12-09 21:22:43
似乎你已经有了它,在[self.milkTypes allKeys对象already :节]中-只需使用它来代替,所以
NSString *country = [[self.milkTypes allKeys] objectAtIndex: indexPath.section];https://stackoverflow.com/questions/8444923
复制相似问题