gpt4 book ai didi

iphone - setIndentationLevel 不适用于 cell.ContentView subview

转载 作者:行者123 更新时间:2023-11-28 22:47:26 24 4
gpt4 key购买 nike

为什么 setIndentationLevel 没有获取单元格 subview ?我有一个 UITableView。我的 cellForRowAtIndexPath 如下所示。我添加的箭头没有随 textlabel 移动。为什么?

我该怎么做?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"small_arrow.png"]];
img.frame = CGRectMake(5, 10, 7, 11);
[cell.contentView addSubview:img];

cell.textLabel.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"name"];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:18];

[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];


return cell;
}

最佳答案

是啊!!!它现在正在工作。我创建了一个自定义单元格,它的 m 文件就像

#import "customecell.h"

@implementation customecell

@synthesize textLbl,img;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

textLbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 320, 20)];
textLbl.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:textLbl];


img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 7, 11)];
img.image = [UIImage imageNamed:@"small_arrow.png"];
[self.contentView addSubview:img];

}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)layoutSubviews{
[super layoutSubviews];
float indentPoints = self.indentationLevel * self.indentationWidth;

self.contentView.frame = CGRectMake(
indentPoints,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentPoints,
self.contentView.frame.size.height
);
}

@end

并且 cellForRowAtIndexPath 更改为

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";

customecell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customecell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLbl.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"name"];
cell.textLbl.font = [UIFont fontWithName:@"Helvetica" size:18];

[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
cell.indentationWidth = 25;

float indentPoints = cell.indentationLevel * cell.indentationWidth;

cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);

return cell;
}

关于iphone - setIndentationLevel 不适用于 cell.ContentView subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837613/

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