0

为什么setIndentationLevel不使用单元格子视图?我有一个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;
}
4

1 回答 1

2

是的 !!!它现在工作。我创建了一个自定义单元格,它的 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;
}
于 2012-10-11T11:41:03.203 回答